CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: fscanf issues

  1. #1
    Join Date
    Feb 2004
    Posts
    38

    fscanf issues

    hello
    i am struggling a bit with fscanf and hope someone can assist me here.

    my goal is to just open a file
    read each value and store and print one of two values START and FINISH

    the trouble i am having is the issue of having only one
    number on a line and not two.

    say i have the following data file
    data.dat
    ----------------
    0
    1 234
    22 3456
    333 4567
    4444 56789
    0
    0

    i just want to read the file and out put
    start = 0 and finish = BLANK
    start = 1 and finish = 234
    start = 22 and finish = 3456

    the problem i am having is that it prints it out mixed up
    the output i get is (where finsih should be it reads the next start)
    start = 0 and finish = 1
    start = 234 and finish = 22
    start = 3456 and finish = 4444

    my code is as follows:

    while ( (c = getc (fPtr)) != EOF){
    fscanf(fPtr, "%d %d", &start, &finsih);
    printf("start= %d and finish= %d",start,finish);
    }

    this also skips the first line

    my question is how do i discern between a line with 2 fields VS a line with 1 field

    meaning can i do a count or check on what fscanf
    brings in or does fscanf return the number of fields it has read

    something like (this is not true code)

    open file
    read file
    while not EOF
    fscanf(%d %d, start, finish);
    if finsih has nothing then dont print out finsih
    else print both out

    please help im quite frustrated at C right now

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    I am not familiar with fscanf().

    There are several solutions. In your code you use reset the temporary start and finish. If the call to fscanf() does not change variable finish, then it is a one-liner.

    Kuphryn

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    I wrote this program and I think it works ( I use fgets and sscanf instead of fscanf):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *fp;
      int START,FINISH,i,count,num_of_ints;
      char *str = (char*)malloc(100);
      fp = fopen("data.dat","r");
      if (fp)
      {
         while (fgets(str,100,fp))
         {
            count = 0;
            // count number of spaces:
            for (i=0;i<strlen(str);i++)
                if (str[i]==' ')
    	count++;
            // find number of integers per line:
            num_of_ints = count + 1;
            if (num_of_ints==1)
           {
             sscanf(str,"%d",&START);
             printf("START = %d   FINISH = BLANK\n",START);
           }
           else
              if (num_of_ints==2)
    	{
    	   sscanf(str,"%d %d",&START,&FINISH);
    	   printf("START = %d   FINISH = %d\n",START,FINISH);
    	}
    
          }		
        fclose(fp);
      }
    }
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    try sscanf...

    If the file is a text file you can also use sscanf to get the numbers on numeric variables. cheking the value of n2 you can see if the 2nd number is missing or not.

    Code:
    #include "stdio.h"
    
    int main()
    {
      FILE* infile = fopen("test.txt","rt");
      char buff[100];
      int n1,n2;
    
      while(fgets(buff,95,infile))
      {
        n2 = -1;
        sscanf(buff,"%d %d",&n1,&n2);
        printf("%d %d\n",n1,n2);
    
        if (n2 == -1) printf("2nd number is missing\n");
      }
      return 0;
    }
    test.txt
    10
    50 60
    70 80
    90
    Last edited by Rabelo; February 13th, 2004 at 11:45 AM.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158
    Ooh, this is really weird. The scanf-family of functions returns the number of items successfully read, so this seems to be the info you need. Tis is the kind of question I tend to answer with RTFM, although it is not very polite ;-)

  6. #6
    Join Date
    Dec 2003
    Posts
    99
    while ( (c = getc (fPtr)) != EOF){
    fscanf(fPtr, "%d %d", &start, &finsih);
    printf("start= %d and finish= %d",start,finish);
    }
    doesnt getc(fptr) eat up one character from the file (similar to fgetc)

    If it doesnt that is great way of avoiding something like ungetc.

    fgets followed by sscanf is the better approach.

    Richard, even if fscanf returns the number of succesffull reads,
    the first fscanf will return two successful reads since it shifts to the second line.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured