1

I've got an input file like this:

1 2 2 Medium 
1 2 0 Medium
2 1 0 Medium
2 0 2 Medium

I am using fscanf() to read in the integer values and that is working fine. does anyone have any ideas how to read in the string and place it in a string variable???

int x,y,z; 
char* cs; 
fscanf(fp3,"%d",&x); 
fscanf(fp3,"%d",&y); 
fscanf(fp3,"%d",&z); 
fscanf(fp3,"%s",&cs);
6
  • I'd be somewhat worried if you weren't reading it into a "string variable" in the first place.
    – WhozCraig
    Commented Mar 2, 2013 at 8:00
  • same just with %s ? and use a char*.
    – Orel Eraki
    Commented Mar 2, 2013 at 8:01
  • Why don't you post the code snippet for the fscanf function call you're using ?
    – Tuxdude
    Commented Mar 2, 2013 at 8:03
  • int x,y,z; char* cs; fscanf(fp3,"%d",&x); fscanf(fp3,"%d",&y); fscanf(fp3,"%d",&z); fscanf(fp3,"%s",&cs); Commented Mar 2, 2013 at 8:05
  • scanning the string doesnt work. im getting run time errors Commented Mar 2, 2013 at 8:07

3 Answers 3

2

You need already allocated space to store the string. Try something like this:

int x,y,z; 
char cs[20];
fscanf(fp3,"%d",&x); 
fscanf(fp3,"%d",&y); 
fscanf(fp3,"%d",&z); 
fscanf(fp3,"%19s",cs);

You can also scan for all the values at once:

fscanf(fp3,"%d %d %d %19s", &x, &y, &z, cs);

N.B: Always use %<length>s in a scanf pattern to prevent buffer overflow.

4
  • 1
    I dont think 'cs' should be with &, it is an array.
    – Orel Eraki
    Commented Mar 2, 2013 at 8:22
  • using strcmp. Although you'll have to do some pointer voodoo on the char array. like, use &cs[0] instead of cs. normal strings which are char* are fine. So: strcmp(str1, &cs[0]);
    – dudeofea
    Commented Mar 2, 2013 at 8:36
  • 2
    Note that you should test the return value from fscanf. It it is not 4 then some of the conversions failed or the end of file was reached.
    – Jens
    Commented Mar 2, 2013 at 9:07
  • You can read the input in a single instruction (I like it better): if (fscanf(fp3, "%d%d%d%s", &x, &y, &z, cs) != 4) /* error */;
    – pmg
    Commented Mar 2, 2013 at 9:30
1

Watch out for two things:

  • You need to reserve a buffer to copy the string into.
  • Also never ever use a plain "%s" scanf pattern. It should be forbidden. Instead insert the length of the buffer, minus 1 between the % and the s. E.g., "%7s" for an 8 byte buffer.
1

2 things to say:

  1. It looks nicer when you use one format string
  2. You should use a loop

Here's some code:

int a,b,c;
char str[256];
while(fscanf(fd, "%d %d %d %s ", &a, &b, &c, str) == 4){
    //get some coffee
}

this is a standard while not End of File loop. Also, str doesn't need & because it already is a pointer and doesn't need to be referenced like a, b or c. The space at the end of the format string, after %s, means it will stop once that one space is read. This way it is not included in the string str.

6
  • 1
    This is undefined behavior; str is uninitialized and points nowhere. That's why the OP gets the unhandled exception.
    – Jens
    Commented Mar 2, 2013 at 9:06
  • 1
    Better test against the number of conversions rather than EOF
    – pmg
    Commented Mar 2, 2013 at 9:32
  • fixed my answer, ran it too.
    – dudeofea
    Commented Mar 2, 2013 at 10:02
  • 1
    you may substitute &str[0] by str
    – V-X
    Commented Mar 2, 2013 at 11:36
  • yea, just ran with str and it works. I wasn't sure if C cared about array pointers vs normal pointers.
    – dudeofea
    Commented Mar 2, 2013 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.