help with fread in C
Results 1 to 9 of 9

Thread: help with fread in C

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    161

    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.

  2. #2
    Member
    Join Date
    Oct 2002
    Posts
    37
    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

  3. #3
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    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

  4. #4

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    186
    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.\"

  6. #6
    Junior Member
    Join Date
    Apr 2003
    Posts
    9
    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();
    }
    Tha all mity Rodent!

  7. #7
    Junior Member
    Join Date
    Apr 2003
    Posts
    13
    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;
    };

  8. #8
    Junior Member
    Join Date
    Apr 2003
    Posts
    9
    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..
    Tha all mity Rodent!

  9. #9
    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
  •