Thread: Reading data from Text File

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Reading data from Text File

    I am trying to read records from a text file. What is the best way to do this? Here is what I did in my program:

    fgets(sentence,500,myfile);

    sscanf(sentence,"%s%s%s%s%s%s%s%s",tempId,tempPric e,tempDate,myOffice,tempSold,tempCon,tempBed,tempA dd);

    I used fgets() to get the target line of text from the text file and save it in my array "sentence". Then i use sscanf to get each piece of data, the problem is that this only works for about 4 of the data members, then it skips one and records the rest. Is this some sort of input overflow? I can't figure out how to get this to work. Is this even the best way to read a text file?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I think we need to see some actual code, plus a couple of sample lines from the file before anyone is going to give a good answer...

    as an early observation you are missing a couple of commas from your sscanf() variables list.
    Last edited by CommonTater; 03-25-2011 at 08:24 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thanks, but my code is really long and I really only need to know if anyone knows a better way to read data from text file and why sscanf would skip data. I think it has something to do with overflow.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check sscanf() return value?
    Post the relevant code. where's the input file? post the sample.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DarrinTE View Post
    Thanks, but my code is really long and I really only need to know if anyone knows a better way to read data from text file and why sscanf would skip data. I think it has something to do with overflow.
    We only need the short code section where you're intializing the variables and calling scanf(), not the whole program.

    The request for some sample lines from the file so we could see how it's formatted.

    fscanf() and sscanf() are perfectly fine ways to read formatted data from a file. But they are very picky about correct formatting and target variable types.

    Frankly, if the 1 line you did post is accurate... I think it has to do with missing commas in your variable list...

    Code:
    sscanf(sentence,"%s%s%s%s%s%s%s%s",tempId,tempPric, e,tempDate,myOffice,tempSold,tempCon,tempBed,tempA, dd);

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You're overthinking again, CommonTater. I'm sure "tempPric e" was supposed to be "tempPrice", and "tempA dd" was supposed to be "tempAdd". Counting the number of formatters and number parameters. Plus the variable names now make sense.

    I'm surprised the compiler let that pass.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    You're overthinking again, CommonTater.
    Sigh... seeing your version, I think you're right....

    I'm sure "tempPric e" was supposed to be "tempPrice", and "tempA dd" was supposed to be "tempAdd". Counting the number of formatters and number parameters. Plus the variable names now make sense.
    Your answer makes a lot more sense than mine...


    I'm surprised the compiler let that pass.
    As am I...

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thanks for the help so far, for some reason when I posted the thread it screwed up the code. Here is some code from my program:
    The text Doc I am trying to read looks something like this:
    LID Price Date Office Sold Constr
    966 10.99 100899 1 n wood
    ...

    int tempId[6];
    int tempPrice[9];
    int tempDate[6];
    int myOffice[4];
    char tempSold[1];
    char tempCon[6];
    char tempBed[3];
    char tempAdd[20];

    char sentence[500];
    FILE* myfile; //creates file pointer
    myfile=fopen("realestate.txt","r+");//opens file for read and write


    fgets(sentence,500,myfile);// gets the sentence I am looking for
    sscanf(sentence,"%s%s%s%s%s%s%s%s",tempId,tempPric e ,tempDate,myOffice,tempSold,tempCon,tempBed,tempAd d );
    *There is not actually a space in tempPrice and tempAdd, for some reason the code gets messed up when I post on here

    The sscanf is getting the values from the arrays I created and storing them into the arrays
    Last edited by DarrinTE; 03-25-2011 at 02:09 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On programming forums, you always need to put your code, between code tags. Just go into the advanced editing window, highlight your code, and click on the # icon top center right on the top bar of the window.

    Now your code will be kept looking like code, and much easier for everyone to read/study.

    ALWAYS use code tags around your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

Tags for this Thread