尝试从我知道的文件中读取该文件,该文件存在并且不错,但是无法打开

时间:2019-04-25 20:20:38

标签: c fopen dspic

当我尝试打开文件“ canbus.txt.txt”时,它将返回一条错误消息,该消息反复读出“错误:无错误”。我找不到这个问题的来历。我的文件在主项目目录中,并且名称和扩展名都正确。

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int i;
char value;
char test[60];
char *line;
int num = 0;
int address[25];
int values[25];

void ms_delay (int N)
{
    T1CON = 0x08030;

    int delay = N * 62.5;   // 1 Milisecond => .001 / (256 * (1/16,000,000)) = 62.5
    TMR1 = 0;
    while (TMR1 < delay);
}

int main (void)
{
    PIN_MANAGER_Initialize();
    UART1_Initialize();
    ECAN1_Initialize();
    ECAN1_ReceiveEnable();
    INTERRUPT_Initialize();

    FILE *fp;
    char* filename = "canbus.txt.txt";
    fp = fopen(filename, "r");

    if(fp == NULL){

        perror("Error");
        exit(EXIT_FAILURE);
    }

    while(fgets(line, sizeof(line), fp)){
        fscanf(fp, "%lf %lf", &address[num], &values[num]);
        sprintf(test, "Value = %lf  Other = %lf", address[num], values[num]);
            int i = 0;
            while(test[i] != '\0'){
                    UART1_Write(test[i]);
                i++;
            }
        ++num;
    }        
    ms_delay(250);
    UART1_Write(0x0D);
    UART1_Write(0x0A);
    fclose(fp);
    return 0;
}

1 个答案:

答案 0 :(得分:-1)

#include <stdio.h>
 int main()
  {
    FILE *fr;
    char c;
    fr = fopen("prog.txt", "r");
    while( c != EOF)
    {
      c = fgetc(fr); /* read from file*/
      printf("%c",c); /*  display on screen*/
    }
    fclose(fr);
    return 0;
    }

了解更多https://www.codesdope.com/c-enjoy-with-files/