Thread: fgetc help

  1. #1
    Registered User
    Join Date
    Apr 2010

    fgetc help

    Hello I was seeing if anybody could help me with the programming of this project...

    I want to scan in data information into my program, the data file is in one file with every bit of information separated by a comma,

    how can i set up a while and for loop so each bit of information will get stored in its proper location then when it comes upon a comma it will to another array

    this is what i have



  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Ok, so put ch into an array, and keep track of where you're at in the array with some sort of counter.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Henderson, NV
    OK there are at least two ways you can approach this, with binary data files or textual data files. Each has its pros and cons.

    Binary data files are probably the easiest and more fool-proof ways of dealing with data files:
    // this is what the record might look like:
    // pretend it is an employee  or a student
    struct TMyDataRecord
         int nAge;
         char achName[20];
         char achAddress[40];
    // assuming the records are all uniform size we can figure out the number
    // of records in the data file....
    // the file is already open...
    fseek (pFile, 0, SEEK_END);
    size=ftell (pFile);
    int nRecordCount = size / sizeof(struct TMyDataRecord);
    fseek(pFile, 0, SEEK_BEGIN);
    int nCount;
    for(nCount = 0; nCount < nRecordCount; nCount++)
         struct TMyDataRecord newRecord;
         fread(&newRecord, 1, sizeof(struct TMyDataRecord)); // read whole record in one go
         // stash the record somewhere, in an array or whatever
    So the upside of a binary file is that its easy to read the records in a failsafe manner. The downside is that it is harder to read/edit the datafile by hand if need be.

    Text files on the other hand can be hand-written if need be; the downside is that they can be more problematic to read.
    Probably what I would do is group each block of data into records, delimiting them with carriage returns so the above would involve a data file that looked (roughly) like:
    21,"Joe Smith", "121 Fooby Lane",
    24, "Jane Doe", "4545 Hacienda Street, apt 5",
    // etc
    In which case you would peel off each record by seeking to the first (next) '\n', then parse each line read by seeking out ','...and as you may have noticed, you hit hard part #1 with the text file as data file paradigm: handling delimiters when they are embedded in the quotes. This further lends weight to the argument of the ease of use of the binary data file where you don't have to worry about it...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  3. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 04:00 AM
  4. fgetc
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 03-03-2004, 08:18 AM
  5. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 05:00 AM
Website Security Test