0

When I try to open a file "canbus.txt.txt" it is coming back with an error message that reads out "error: No error" repeatedly. I cannot find where this issue would be coming from. My file is in the main project directory and the name and extensions are all correct.

#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;
}
dfetz
  • 27
  • 1
  • Where do all your functions come from? The `_Initialize()` calls? Are they necessary to reproduce the bug? Also, is your program exiting from that `if (fp == NULL)` block? – einpoklum Apr 25 '19 at 20:28
  • 1
    Are you coding for a PIC microcontroller? Why would you expect `fopen` call to succed on a microcontroller? As for `error: No error` - the implementation of `fopen` probably doesn't set `errno` to anything. – KamilCuk Apr 25 '19 at 20:31
  • "Why wouldnt it work on a microcontroller? – dfetz Apr 25 '19 at 20:53
  • fopen() works in a microcontroller with the heap. You could not work with a file which exists in you project!!! – Mike Apr 26 '19 at 09:36
  • Have you checked if it because of the double extension `"canbus.txt.txt"`? Just a hunch. – Elysian Storm Apr 26 '19 at 19:42

1 Answers1

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

To know more https://www.codesdope.com/c-enjoy-with-files/