Thread: reading line including whitespace to string

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    reading line including whitespace to string

    i have an input file that looks like this (intentional whitespace);
    456
    Unspecified user input up to 100 characters
    X 4 5 6
    X 7 8 9
    X 1 2 3

    My code runs at the moment using fscanf which gets the user input as long as there is no whitespace (unspecified_user_input etc.)

    However, I cannot seem to get it to work with fgets which is what google has told me to use.

    Currently using;
    Code:
    fscanf(input, "%s", &desc);
    printf("Desc is %s\n", desc);
    Also tried
    Code:
    while(fgets(buf, 100, input) != NULL)
    {
    fscanf(input, "%s", &desc);
    printf("Desc is %s\n", desc);
    }
    and I have also tried multiple variations I have found online.

    Is there any way to tell fscanf to read until a new line character?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should take sscanf to read from input string, not fscanf to read from file.
    Look at the formatspecifiers for *scanf, you can tell it, to read until '\n'.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your input is rather confusing, but there are many ways to do this. You could just read one character at a time with fgetc and compare it to \n.


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

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by browser View Post
    i have an input file that looks like this (intentional whitespace);
    456
    Unspecified user input up to 100 characters
    X 4 5 6
    X 7 8 9
    X 1 2 3

    My code runs at the moment using fscanf which gets the user input as long as there is no whitespace (unspecified_user_input etc.)

    However, I cannot seem to get it to work with fgets which is what google has told me to use.

    Currently using;
    Code:
    fscanf(input, "%s", &desc);
    printf("Desc is %s\n", desc);
    Also tried
    Code:
    while(fgets(buf, 100, input) != NULL)
    {
    fscanf(input, "%s", &desc);
    printf("Desc is %s\n", desc);
    }
    and I have also tried multiple variations I have found online.

    Is there any way to tell fscanf to read until a new line character?
    Are you simply trying to read the entire line? If so you've already done that with fgets(), unless you have to do some kind of processing on the data afteward, your job is done, no need for fscanf() which will displace your file pointer or sscanf() which will end you back up at reading only to the first space. If you need to keep the data you can use strcpy() or strdup() to pull it out of your buffer into an array...

    If you do need to do post processing you need to set up a proper format string in sscanf() to pull the data out of your buffer.

    Your current loop is going to land you up in the wrong place because it displaces your file pointer.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Thanks for all the suggestions, but I am still having trouble.

    I have tried

    Code:
    fscanf(input, "%[^\n]", &desc);
    and

    Code:
    while(fgets(dump, 100, input) != NULL)
    {
    sscanf(dump, "%s", &desc);
    }
    and neither have worked.

    Where am I going wrong?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you post up more of your code, we can see what's wrong. The little snippets of code you post, don't let us see everything we need to see, to say what could be wrong.
    Last edited by Adak; 10-16-2011 at 11:51 AM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Thanks guys, got it working again. I think I was getting confused with the "while" part of the code and it is now working without that.

    And sorry Adak, next time I will try to post more of the code.

    Thanks

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll hold you to that Browser, and you're welcome.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Okay guys, I may have broken it again.

    Quick recap, I am trying to read filename.xyz

    Code:
        4
    User description
    X   -0.906801    0.569837    0.066005
    X    0.956585   -0.258900    0.342064
    X   -0.962633    0.494832    0.380356
    X   -0.259093    0.292540    0.854958
    where the user description is up to 100 characters.

    I am using the following code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    
    /*Start the programme*/
    int main()
    {
      FILE* input;
      int N=0;
      double T;
      int i, j;
     
     
      input=fopen("filename.xyz", "r");
      fscanf(input, "%d", &N);
      printf("N has been read as %d\n", N);
    
      char buf[100];
      
      fgets(buf, 100, input);
      printf("Description is %s", buf);
      
     
      double array[N][3];
      double **ptr;
     
      ptr=malloc(N*sizeof(double*));
      
      i=0;
      for (i=0;i<N;i++)
      {
        ptr[i] = malloc(3*sizeof(double));
      }
      
      char type[N];
      
      i=0;
      for(i=0;i<N;i++)
      {
          fscanf(input, "%s", &type);
          printf("type is %s\n", type);
          j=0;
          for(j=0;j<3;j++)
          {
          fscanf(input, "%lf", &array[i][j]);
          }
       }
      
      fclose(input); 
      
      exit(EXIT_FAILURE);
    }
    In my output 'user' appears as type[0] and 'description' appears as type[1] before having 'X' correctly as type[2] and type[3].

    I would guess that I am using fgets incorrectly?

    Basically I want to do the following;
    1) Read N from line 1 (ignoring leading whitespace) and store in N
    2) Read entire line 2 and store as character string which I can output later. (read until newline?)
    3)For next 'N' lines, store first character as type, and next 3 doubles in array[i][0], [i][1], and [i][2].

    Could you please tell me what I am doing wrong with the description and whether the rest of the code is correctly allocated memory etc.

    Thanks and I hope this is enough code for you Adak.

    I also tried
    Code:
     while(fgets(buf, 80, input) != NULL)
      {
        puts(buf);
      printf("Description is %s", buf);
      }
    which at least keeps user description together...but also prints the rest of the file under 'buf' (I guess because of the != NULL)?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's one thing wrong:

    Code:
          fscanf(input, "%s", &type);
    "type" is an array, so don't use the & address of operator with it. Your compiler should be warning you about stuff like that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Thanks, I changed that and my output still looks weird, but so far is the only one that has printed 'user description' in some form to the screen

    Code:
    N has been read as 4
    
    
    Description is 
    user description
    
    Description is user description
    O   -0.906801    0.569837    0.066005
    
    Description is O   -0.906801    0.569837    0.066005
    O    0.956585   -0.258900    0.342064
    
    Description is O    0.956585   -0.258900    0.342064
    O   -0.962633    0.494832    0.380356
    
    Description is O   -0.962633    0.494832    0.380356
    O   -0.259093    0.292540    0.854958
    
    Description is O   -0.259093    0.292540    0.854958
    type is ��L�
    type is ��L�
    type is ��L�
    type is ��L�
    that was using

    Code:
    while(fgets(buf, 80, input) != NULL)
     {
       puts(buf);
     printf("Description is %s", buf);
     }

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    So I 'fixed' it by doing this;

    Code:
    fgets(buf, sizeof buf, input);
      N=atoi(buf);
      printf("N has been read as %d\n", N);
      fgets(desc, sizeof desc, input);
      printf("Description has been read as %s", desc);
    Is that acceptable or is it just a temporary fix?

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by browser View Post
    Is that acceptable or is it just a temporary fix?
    That's fine, altho you could have kept the fscanf for N I think. The issue with "Description" (which was hard to understand until you posted the output) was because you used a loop, whereas that only needs to be read once, right?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by MK27 View Post
    That's fine, altho you could have kept the fscanf for N I think. The issue with "Description" (which was hard to understand until you posted the output) was because you used a loop, whereas that only needs to be read once, right?
    Yes, it just needed to be read once. But I think the problem was that after fscanf, fgets still went to the beginning of the file to read again. If I changed the '4' to an 'X' the fgets still printed it, although I don't know if that is because my fscanf specified an integer or not...

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Sorry guys, I have spotted another mistake in the code. The above was working fine because all the types ("X") in the the file were the same. It turns out if they are different like here;

    Code:
     4
    Description
    O   -0.912859   -0.502928    0.184167
    P    0.044206   -1.267539    0.055795
    Q    0.186147   -0.129561   -0.032348
    R   -0.177115   -0.705996    0.996024
    type only holds the last character. I think it has something to do with the memory allocation but I am not sure what to do

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    
    /*Start the programme*/
    int main()
    {
      FILE* input;
      int N=0;
      int i, j;
     
     
      input=fopen("filename.xyz", "r");
      
      char desc[100], buf[10];
      
      fgets(buf, sizeof buf, input);
      N=atoi(buf);
      printf("N has been read as %d\n", N);
      fgets(desc, sizeof desc, input);
      printf("Description has been read as %s", desc);
      
     
      double array[N][3];
      double **ptr;
     
      ptr=malloc(N*sizeof(double*));
      
      i=0;
      for (i=0;i<N;i++)
      {
        ptr[i] = malloc(3*sizeof(double));
      }
      
      char type[N];
      
      i=0;
      for(i=0;i<N;i++)
      {
          fscanf(input, "%s", type);
          printf("type is %s\n", type);
          j=0;
          for(j=0;j<3;j++)
          {
          fscanf(input, "%lf", &array[i][j]);
          }
       }
       
       i=0;
       for(i=0;i<N;i++)
       {
    	   printf("type has held as %s\n", type);
       }
      
      fclose(input); 
      
      exit(EXIT_FAILURE);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  2. reading a string of hex values from command line
    By peeweearies in forum C Programming
    Replies: 7
    Last Post: 02-24-2009, 02:25 AM
  3. Q: How can you store whitespace in a string?
    By barrie in forum C Programming
    Replies: 7
    Last Post: 01-05-2007, 09:09 AM
  4. Reading a line including blanks
    By Ec4U2du in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 07:32 PM
  5. Extracting Whitespace from String
    By bob2509 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2002, 11:27 PM