#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Exclamation Bit Of help with file handling


    Hey guys i'm trying to read some data from a file in c
    the file contain's a string and two int's separated by : .
    but the string takes the value of the whole line and the two int are 0 any toughts

    im using a xcode if that makes a diffrence??

    here's the code

    Code:
    vertnode * InsertFromFile(){
    	FILE *fp;
    	char name[100], line[200];
        int xCo_ord , yCo_ord ;
    	vertnode *head =  NULL , *temp;
    
        if ((fp = fopen(filename,"r")) != NULL) {
            
            while (!feof(fp)){
                fgets(line , 200 , fp );
                
                sscanf(line,"%s:%d:%d. ",name,&xCo_ord,&yCo_ord);
                
                if (head == NULL) {
                    head = getvert();
                    //head->name  name;
                    head->xCo_ord = xCo_ord;
                    head->yCo_ord = yCo_ord;
                } else {
                    temp = head;
                    while (temp->nextvert != NULL){temp = temp->nextvert;}
                    temp->nextvert = getvert();
                    temp = temp->nextvert ;
                    //temp->name name;
                    temp->xCo_ord = xCo_ord;
                    temp->yCo_ord = yCo_ord;
                }
            }
            printf("File Loaded!");
            
        } else {
            printf("Error Reading File");
        }
        
            
            return head;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    Replace the first %s like so
    sscanf(line,"%[^:]:%d:%d. ",name,&xCo_ord,&yCo_ord);

    Also, your use of feof() is wrong as well.
    http://sourceforge.net/apps/mediawik...php?title=Feof

    You can read the file and check for end of file in by using fgets, like so
    while ( fgets(line , sizeof(line) , fp ) != NULL )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    What's the return value from sscanf? It should be 3 if all three conversions succeeded. Maybe you should check that.

    I'm assuming that the fgets succeeded, but you could check its return value to make sure.

    What's the format of the name field? Is it just one word that doesn't contain a colon? Or is it more than one word; ie, does it contain spaces? For scanf'ing a %s, a space acts as a delimiter so that name would just contain the first word in the string and the subsequent attempts to convert a character string into two ints would fail, leaving the two integer variables unchanged. Perhaps if you were to show us a few lines of data from the file, because as it is we have no way to test it.

    Have you tried running this in the debugger?

    PS
    What salem said. Just because you have a colon as a delimiter in the format string is no guarantee that a string conversion will stop when it hits a colon. That makes more sense out of the symptom you described.
    Last edited by dwise1_aol; January 17th, 2013 at 03:41 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Talking Thanks!


    Originally Posted by salem
    Replace the first %s like so
    sscanf(line,"%[^:]:%d:%d. ",name,&xCo_ord,&yCo_ord);

    Also, your use of feof() is wrong as well.
    http://sourceforge.net/apps/mediawik...php?title=Feof

    You can read the file and check for end of file in by using fgets, like so
    while ( fgets(line , sizeof(line) , fp ) != NULL )
    Thank you thank you THANK YOU!!
    In my third year of a Software Development course, was about to give because this simple bit of code was giving me so much hearth acke.. :thumbs:

IMN logo majestic logo threadwatch logo seochat tools logo