C Programming - How to Read Files

+2 Samuel Egiefameh · February 24, 2015
Hello everyone, I am having a compiling error in one of my codes. In tutorial video 51 from Bucky teaching C programming, I copied the code exactly as written in the video, yet my code does not seem to compile. I'm trying to understand how to read files line by line into my program. Can anyone identify the error? Thank you in advance as you do so.



#include<stdio.h>
#include<math.h>
#include<Windows.h>
#include<stdlib.h>


int main()
{

FILE* fPointer;
fPointer = fopen("file.txt", "r");
char singleLine[150];

while(!feof(fPointer))
{
fgets(singleLine, 150, fPointer);
puts(singleLine);

}

fclose(fPointer);
return 0;

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · February 24, 2015
what does your compiler say?
0 Samuel Egiefameh · February 24, 2015
This is what my compiler says:  I am using visual studio C++  2010 Express


1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(12): error C2143: syntax error : missing ';' before 'type'
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(16): error C2065: 'singleLine' : undeclared identifier
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(16): warning C4047: 'function' : 'char *' differs in levels of indirection from 'int'
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(16): warning C4024: 'fgets' : different types for formal and actual parameter 1
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(17): error C2065: 'singleLine' : undeclared identifier
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(17): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
1>c:\eegr 409\dumbs\writing_files\writing_files\writing_files.c(17): warning C4024: 'puts' : different types for formal and actual parameter 1
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0 Dol Lod · February 28, 2015
I can offer an alternative approach to read a file or binary file. It is tested and used in actual code. Use the fread function with r or br depending on the file type or use the read function for simple commands. 
0 Samuel Egiefameh · March 9, 2015
Thank you so much.
  • 1

C

107,193 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator