c - 尝试读取我知道存在且正常但无法打开的文件

标签 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;
}

最佳答案

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

关于c - 尝试读取我知道存在且正常但无法打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857053/

相关文章:

c - 微型计算机,串口。请求多个字节

c++ - 怎么把电脑画的图旋转90度

c - 如何在 C 中打开文件 (fopen/fread)

c - 两个进程同时调用 open,一个 "W",另一个 "r"。发生错误?

c - fopen 后检测文件删除

c - UART 不传输/打印超过一个字符

bit-manipulation - 如何旋转一个字中的位

c - 为什么阶乘是13!不适用于 unsigned long long int 吗?

c - 使用 unix 函数在 OSX 下移动/复制文件

c - 使用 fprintf() 在 C 中写入文本文件