trouble scanning in... and link listing

This is a discussion on trouble scanning in... and link listing within the C Programming forums, part of the General Programming Boards category; Hello, I have this file with input data it looks like this: Code: Ann I. 103 Ann X. 11 Jim ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    trouble scanning in... and link listing

    Hello, I have this file with input data it looks like this:

    Code:
    Ann I.
    103
    
    Ann X.
    11
    
    Jim C.
    106
    
    Ann B.
    123
    
    John LongLastName
    143
    
    class size 3
    
    drop
    11
    103
    106
    I'm having trouble figuring out how I can scan this in.... I have defined a structure data type like this

    Code:
    typedef struct  student {
             char name[11];                          
             int ID;                           
             struct student* studentPtr;      
             } STUDENT;
    What I want to do is create a stack using linked lists with the student name in the .name member and the number under the student name in the .ID member

    but I have extra information in my input file that I do not need to scan in yet, and I don't want it in my linked lists.... I'm having trouble figuring out how to make a loop that will stop after the last student.....

    in this case:
    Code:
    John LongLastName
    143
    I've declared these pointers to files
    Code:
                     FILE *fin, *fout;
                     fin = fopen("cp5.in", "r");
                     fout = fopen("cp5_test.txt", "w");
    i've been trying different fscanf parameters and while loops... but they all go pass the student names and IDs data

    any suggestions?

    Thank You

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They all will too*. How do you know when you have run out of studends? You either:
    a - See that 'class size' is in the list, and that's something no one will be named, so you stop.
    b - See that 'some name' + 'some number' sequence has not been read.
    c - See that you've read past a set N number of students.
    d - Etc, etc.

    It's up to you to figure out what fits your data, and when you don't reach that, stop reading, or handle your incorrect data.

    *You can do it with scanf, and test the arguments of it, but for the most part, just single scanf calls isn't going to be enough.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I was thinking that I could use something like

    Code:
    while( fscanf(fin," %s ", temp_name) != "Class Size" ) {
    But, wouldn't the string that it reads in be
    Code:
    class size 3
    which is != to class size... so it would keep going.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I tried using that while statement it wouldn't work for me

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I've been trying to figure this out for hours, it seems a lot tougher than it should be...

    i can't do a loop like this:

    Code:
    while( fscanf(fin," %s ", temp_name) != "Class Size" ) {
    because fscanf will never = Class Size since it only scans in Class....

    so then if I change it to

    Code:
    while( fscanf(fin," %s ", temp_name) != "Class" ) {
    This will work, but it leaves my pointer fin at an awkward position, every time
    it runs through the loop it scans in one word to compare and leaves me after the first name
    when I need to read the entire first name and laste name into a string

    I would like to use fgets to input the whole string, but I can't at this point cause I'm after the first name....

    I've tried using strncmp.... but end up with similar problems

    If anyone has any idea how I might get around this, please help me

    Thank you

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I was also thinking maye checking it with that while loop and fscanf and then resetting the pointer fin to the beginning of that line and reading in the data.... but I dont know how to reste it.... rewind would reset it back to the beginning of the whole file......

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't compare strings with any equality operators. You have to use the string library, or roll your own, to test strings.
    Code:
    #include<string.h>
    if( strcmp( somebuffer, "Hello world!" ) == 0 )
    {
        printf("The buffer 'somebufffer' contains an exact match for \"Hello world!\"\n" );
    }
    Look in your book of choice, or man page, or what not at the str* functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    but the strncmp function returns a 0 if two strings are equal....

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I have a follow up question....

    I think I'm going to go with a do while loop structure to read in my data. for my input I can only accept names up to 10 char long... so I can compare my input with "class siz" and as long as the student name does not equal "class siz" I'm ok

    so I was thinking:
    Code:
    do{   fgets( temp_name,10,fin);
    
    
         } while(strncmp( "class siz",temp_name) != 0 );
    but my question is... this leaves my fin pointer pointing at 11 char into the student name...
    and when I run a fscanf it will start scanning in the name... when I want it to go to the 2nd line and get the student number.....

    how can I make it scan the number under the the name, after it runs fgets.... i want it to go to the next line after fgets.....


    Thank you

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgets with a larger buffer to read the whole line, and then use strstr to see if it contains 'class siz'.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    or would it be better to have the end of the do while say

    Code:
    while( temp_name != "class siz" );
    and leave the strncmp out of this

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I do not know what a buffer is. Could you tell me what that is? I see people type it a lot.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    ahhh so do the same "do while" loop i have but in the while part you suggest to add the strstr ....

    something like

    Code:
    while( strstr(temp_name, "class") == NULL );
    at that point I can just say class right not class siz, since class would be in the other string?

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I see how the do while can work.... could you tell me how I might be able to scan the next line instead of continuing to scan in the same line a name is in ... what I mean is, I am only scanning in 9 characters, so when I scan in a long name my pointer gets left in the middle of the name when I implement the scanf to get the ID number under the name, when the name is short, no problem it goes to the next line by itself

    Code:
    fgets(temp_name,10,fin);
    
    printf("%s",temp_name);
    
    
    fscanf(fin,"%d",&ID);
    
    printf("%d",ID);

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    while( temp_name != "class siz" );
    You can't do that with C strings (character arrays). You have to use str[n]cmp().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21