-
March 20th, 2003, 02:30 AM
#1
Senior Member
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.
-
March 20th, 2003, 02:48 AM
#2
Member
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
The only limit a person has, is the limit they give themselves.
Cogito ergo sum. - Descartes
-
March 20th, 2003, 02:58 AM
#3
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!
\"When you say best friends, it means friends forever\" Brand New
\"Best friends means I pulled the trigger
Best friends means you get what you deserve\" Taking Back Sunday
Visit alastairgrant.ca
-
March 20th, 2003, 12:04 PM
#4
-
March 20th, 2003, 01:26 PM
#5
Senior Member
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++
Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"
-
April 14th, 2003, 05:26 AM
#6
Junior Member
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();
}
-
April 15th, 2003, 09:04 AM
#7
Junior Member
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;
};
-
April 16th, 2003, 04:39 AM
#8
Junior Member
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..
-
April 16th, 2003, 05:22 AM
#9
Banned
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;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|