So I've got a directory called temp
temp
It contains:
assets bin src include Makefile
The assets directory contains a "file.txt" file, which is filled with some keyboard alphabetic characters.
My src folder also includes the file "file.c" which is
#include "file.h"
int main(int argc, char * argv[] )
{
initscr();
noecho();
cbreak();
char ch;
FILE * ptr;
if(strcmp(argv[1],"file.txt") == 0)
{
ptr = fopen("file.txt","r");
if(ptr == NULL)
{
mvprintw(0,0,"Error reading the file\n");
refresh();
exit(1);
}
else
{
while( ( ch =fgetc(ptr) ) != EOF)
mvprintw(0,0,"%c",ch);
refresh();
}
}
else
{
mvprintw(0,0,"you didn't specify the right .txt file");
exit(1);
}
endwin();
fclose(ptr);
return 0;
}
The makefile contains:
all:
gcc -Wall -pedantic -std=c99 -Iinclude src/file.c -o bin/runMe -lncurses
However after successful compilation, going to the bin directory and typing ./runMe file.txt
ends up printing nothing new on the screen (doesn't print the characters "abc" which are in the text file (located in the assets directory).
What's going wrong?
If you're running your executable from within the bin directory, then you are trying to open your file in the bin directory, where it isn't.
Run the file from temp, as ./bin/runMe file.txt where the fopen line reads "assets/file.txt"
You have to be explicit where you run a program and where a file is.
You are reading one character at a time and printing these characters at the same location (0, 0). I assume you should see the last character containd in your file at location 0, 0 of your window. Why not using printf?
[EDIT] As you print characters, increment the x location.
int x= 0;
while( ( ch =fgetc(ptr) ) != EOF) {
mvprintw(x,0,"%c",ch);
x++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.