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?Code:/* A program to test file input using C */ #include "stdio.h" #include "string.h" int main(void) { /* Open test.txt file for reading */ FILE *fp; fp=fopen("c:\\test.txt", "r"); //fprintf(fp, "Testing...\n"); /* Declare a string */ char string[20]; /* 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 */ return 0; }
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 greatCode:: 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



LinkBack URL
About LinkBacks


