Hi, wondering if anyone out there can help me with this.
I'm learning C and having a bit of trouble getting to grips with taking inputs from external files.
I made a .txt file, test.txt, which simly contains the word: working
I'm now trying to make a program in C that will open the txt file, read the word "working" into a string and print it back to me on the screen.
Here's the code I have so far:
The program has no trouble opening the file, but when I declare the string the errors begin... Anyone know where I'm going wrong here?
/* A program to test file input using C */
/* Open test.txt file for reading */
/* Declare a string */
/* Input data from file into string */
fgets(string, 20, fp);
/* Print string */
printf("This program is: ");
printf( "String: %s", string);
/* Wait for user's key press, then close */
If it helps, here are the errors I'm coming up with:
Once I get the hang of this I was hoping to be able to read from a list in a txt file (e.g. a list of names) and put each name into a string. If anyone could point me in the right direction with that as well it would be great
: error C2143: syntax error : missing ';' before 'type'
: error C2065: 'string' : undeclared identifier
: warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
: warning C4024: 'fgets' : different types for formal and actual parameter 1