-
help with fread in C
I need to write a function in C that reads a small text file , stores every character in a variable, and then stops reading when it reaches EOF. I assume I need to use fread();
I need precise info on my problem, I have been searching around only finding which parameters go into fread(); but no luck on how to loop until end of file.
Any help will be greatly appreciated.
-
I'm not much on programming, but here is a starter.
do
{
what ever crap you need in here
} while !EOF
that should help a little until someone else can reply
sorry its not more
-
I find it easier to use the fscanf function, which is similar to the scanf function used to read from standard in.
First you need to open a file stream (this opens test.txt for read-only)
FILE * pFIn = fopen("test.txt", "r");
This move the file pointer to the start of the file.
fseek(pFIn, 0L, SEEK_SET);
This scans a character from the file and stores it in the myChar variable of type char.
iVal = fscanf(pFIn, "%c", &myChar);
You would then loop while iVal does not equal EOF. iVal normally contains the number of bytes read or EOF on end of file
FILE * pFIn = fopen("test.txt", "r");
fseek(pFIn, 0L, SEEK_SET);
iVal = fscanf(pFIn, "%c", &myChar);
while(iVal != EOF){
//Do stuff
iVal = fscanf(pFIn, "%c", &myChar);
}
Hope this helps!
-
-
Maybe something like this would work.. I cant remember my c all that well but something like this would work in c++ and c.
Use an array of n size.
i=0
while scanf array[i]
i++
-
sry i didn't go in detail, it's 1:00 am, and i really should get some sleep
#define MAX 200 \\ put whatever here...
char text[MAX];
(open file)
for ( t = 0; t < MAX ; t ++)
{
fscanf( FILE, TYPE, &text[t] )
}
<--- 10 min later -----> (edited)
here's a simple program that should do what you ask
#include <fstream.h>
main()
{
ifstream fin("Msdos.sys"); // ignore msdos.sys and replace it with the text file
cout<< "thingy's\n\n\n";
char ch;
int t = 0;
char text[];
while(fin.get(ch))
text[t] = ch;
cout << ch;
t++;
fin.close();
}
-
using fgetc() function is also an option.
and not to make your programme more dynamic use linked list to store the inputs of the file
stuct some_name{
char file_data;
struct some_name *next;
};
-
fgetc may be a little tricky for him to use.. fgetc just calls a character, but he wants to change all the characters into a variable..
-
You also can try by using struct of array and combine it with linked list ,use some pointer
I hope you understand what i mean ok
Struct bla_bla_bla{
Char data;
int x;
struct data *next;
}*head,*curr,*tail;