0

当我尝试打开文件“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;
}
4

1 回答 1

-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/

于 2019-04-25T20:35:02.723 回答