Thread: Sscanf modifing another value

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    10

    Sscanf modifing another value

    Hi!, i'm new with c, sorry if it's a newbie question, but i can't find an answer.

    I have some Ints declared, and after that I am using a sscanf (with some buffers), but sscanf is modifing the previously declared ints, why that is happening?
    Here is my code:
    Code:
    ...
            int xivn=0;  //here i declare the ints (i never modify this values)
    	int xivt=0;
    	int xiv=0;
    	int xivf=0;
    	
    	if (archivo!= NULL){
    		char linea [128];
    		char resto0 [20];
    		char resto1 [20];
    		char resto2 [20];
    		while (fgets(linea,sizeof(linea),archivo)!=NULL){
    			sscanf(linea,"%s %s %s %s", c0, resto0, resto1, resto2); //if i comment this line i can see the values of the ints unmodified  
    		}
    		fclose(archivo);
    	}else{
    		perror(nombrearchivo);
    		return 1;
    	}
    	printf("xivn:%d, xivt:%d, xiv:%d, xivf:%d \n\n",xivn,xivt,xiv,xivf); //it prints xivn:0, xivt:47, xiv:1919221868, xivf:1953326451  :(
    I'll really appreciate if someone can help me.
    Thank you!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    sscanf(linea,"%s %s %s %s", c0, resto0, resto1, resto2);
    Did you forget c0?

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Hi Adak, thank you for your response!,
    i forgot to paste it here, but it's in my code:

    char c0[2];
    FILE *archivo = fopen (namefile,"r");

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like using a slightly different syntax for this fgets line of code, but I'm not confident that's the problem here. Give it a try:

    Code:
    while (fgets(linea,sizeof(linea),archivo)!=NULL){
        sscanf(linea,"%s %s %s %s", c0, resto0, resto1, resto2); //  
    }        
    //change that to:
    while ((fgets(linea,sizeof(linea),archivo))!=NULL){
        sscanf(linea,"%s %s %s %s", c0, resto0, resto1, resto2); //
    }
    And double check that your string variables are long enough to handle the size of the strings you're handling, including the end of string markere: '\0', which you can't normally see, but have to allow space for.

    After that, I'd have to run the code to see what's up.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    nop, the same problem

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, I don't see it there - you'll have to post more code - preferably a small whole program that I can run, to see the problem.

    Have you checked the returned value of sscanf() yet?
    Last edited by Adak; 10-15-2010 at 04:44 AM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    It's a small version but with the problem:

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char* argv[]){
    	char c0[2];
    	FILE *archivo = fopen ("teapot_uv.obj","r");
    	int xivn=0;
    	int xivt=0;
    	int xiv=0;
    	int xivf=0;	
    	if (archivo!= NULL){
    		char linea [128];
    		char resto0 [20];
    		char resto1 [20];
    		char resto2 [20];
    		while ((fgets(linea,sizeof(linea),archivo))!=NULL){
    			sscanf(linea,"%s %s %s %s", c0, resto0, resto1, resto2);
    		}
    		fclose(archivo);
    	}else{
    		perror("teapot_uv.obj");
    		return 1;
    	}
    	printf("xivn:%d, xivt:%d, xiv:%d, xivf:%d \n\n",xivn,xivt,xiv,xivf);
    	return 0;
    }
    Thank you for your patience!

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No problem - I'm intrigued actually. Back soon.

    There are some words inserted into the file. Hmmmmm!
    Last edited by Adak; 10-15-2010 at 05:09 AM.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Yes, there are comments, my idea is to "pass" that comments using c[0]!='#';

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In the data, c[] becomes "vt". Since c is only sized for two char's, it can't fit the end of string char. Also, there are several places in the header where the arrays are running out of bounds.

    The program runs on, in that early part, but it's entirely lucky. What I've done in the past is just what you mention - if linea[0] == '#' then don't sscanf() that line.

    Do that, and make c[] 3 or 4 char's, and that should help a lot.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If the first word on every single line is anything other than a single character, then you have a buffer overflow, which will either trash variables or crash the program.

    For safety, each sub-string should be as long as the buffer you pass to fgets()
    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.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I just ran through the data, and c[3] works OK with the file you provided, once you add the
    Code:
    if(linea[0] != '#') {
      sscanf() all in here
    code.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    10
    Problem solved, Thank you Adak and Salem!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf question
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 03-08-2010, 10:17 AM
  2. sscanf error
    By roaan in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 06:44 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. Simple sscanf mystery
    By registering in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 11:47 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM

Tags for this Thread