Altera_Forum
Honored Contributor
20 years agoC program reads from .txt file - error!
This is a real beginner question!
I have a simple C program that reads from a txt file. I wrote it in the NIOS II IDE. The txt file is in the same directory on my hard drive as the C file. It's a real simple program - basically it reads from the file one character at a time, and outputs the characters to the screen. When I build the project, the txt file is added to the project list. However, when I run it on the hardware (a Stratix), it doesn't work. So I suppose it's not actually transferring the txt file to the device. Do I manually have to transfer the .txt file to the FLASH memory? How do I do this in the IDE? How do I have to modify my C code to find the txt file in the Flash memory? Thanks a megabyte! Studi ps: here is my C program, to be thorough: ------------------ # include <stdio.h> int main (void) { FILE *input; char c; //open file for reading only input = fopen("input.txt", "r"); //check input file for errors - probably file does not exist if (input == NULL) { fprintf(stderr, "Error - can not open file\n"); return 1; } else { printf("File opened successfully. Contents:\n\n"); while(1) { /* keep looping... */ c = fgetc(input); if(c!=EOF) { printf("%c", c); /* print the file one character at a time */ } else { break; /* ...break when EOF is reached */ } } printf("\n\nNow closing file...\n"); fclose(input); } return 0; } ------------ The output is: Error - can not open file