Thread: Better scanning method

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Better scanning method

    Say I had a file with the following values in it

    100 200 300 400 500

    Now I want to assign a to 100, b to 300 and c to 500. Am I correct in thinking that fscanf has to scan through each one like this?

    fscanf(file,"%d %d %d %d %d",&a,&b,&c,&d,&e);

    There has to be a better way of doing that,without having the needless variables!!!
    PuterPaul.co.uk - Portfolio site

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If that works, why don't you just use it? The whole idea of high level languages like C, is to not write code when someone else has done it for you.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Here's the way:

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int main()
    {
    	FILE *fin= NULL;
    	int Numbers = 0;
    
    	fin = fopen("numbers.txt", "rt");
    
    	while (fscanf(fin, "%d", &Numbers) != EOF)
    	{
    		printf("\nNumbers = %d", Numbers);
    	}
    	getch();
    	return 0;
    }
    Have phun!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Sean
    If that works, why don't you just use it? The whole idea of high level languages like C, is to not write code when someone else has done it for you.
    ...unless you can do it better, of course
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    What i'm basically trying to say is that I don't need 200 and 400 so why waste the space by declaring extra variables, especially when in some cases there can be 20+ values in a file and I only want to use 4 of those values for the program. Thanks GaPe but there doesn't seem to be a conio.h in linux
    PuterPaul.co.uk - Portfolio site

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    The "conio.h" library is just for the "getch()" command and it is used to stop the program. In this case it is useless.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    So does that answer my problem then, taking the conio and getch out of your method just make the program print all the numbers.
    PuterPaul.co.uk - Portfolio site

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Cool

    Yes.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    you could try this:

    Code:
    fscanf(file,"%d %*d %d %*d %d", &a, &b, &c);
    the * will cause the input field to be skipped and no assignment will be made.

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    This methode is inefficient. If you want to save your numbers than you should do this with an array of integers.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    Originally posted by GaPe
    This methode is inefficient. If you want to save your numbers than you should do this with an array of integers.
    his original question was this:

    Originally posted by pdstatha
    Say I had a file with the following values in it

    100 200 300 400 500

    Now I want to assign a to 100, b to 300 and c to 500. Am I correct in thinking that fscanf has to scan through each one like this?

    fscanf(file,"%d %d %d %d %d",&a,&b,&c,&d,&e);

    There has to be a better way of doing that,without having the needless variables!!!
    mine answers that. yours does not. whether your code is more efficient than what i gave him doesnt really matter in this case.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    besides, what does yours really do? if he wanted to store the numbers in an array of int, he could do something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h> //for strtol()
    #include <string.h> //for strlen()
    #include <ctype.h> //for isdigit()
    
    int main(void)
    {
       char  line[1024]; 
       char *endp;
       int   iArray[100] = {NULL};
       int   i, j = 0;
       
       FILE *fileptr;
       
       if((fileptr = fopen("filename.txt", "r")) == NULL)
       {
          printf("Could not open file!\n");
          return 0;
       }
    
       
       while(fgets(line, sizeof(line), fileptr))
       {
          line[strlen(line) - 1] = '\0'; //removes the '\n'
    
          for(i = 0; i < strlen(line); ++i)
          {
             if(isdigit(line[i]))
             {
               iArray[j++] = strtol(&line[i], &endp, 10);
               while(isdigit(line[i])) ++i;
             }
          }
       }
       for(i = 0; i < j; ++i)
          printf("%d ", iArray[i]);   
    
       fclose(fileptr);
       getchar(); //not needed if run from command prompt
       return 0;
    }
    EDIT: bah, i should really compile these things first.
    EDIT2: oops, didnt include 0 as a number that may show up in the file... not good... fixed now.
    Last edited by CtrlAltKick; 03-30-2002 at 10:29 AM.

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I was just showing him the way how to display the numbers. If you want to save them here's the way of doing it:

    Code:
    # include <stdio.h>
    # include <conio.h>
    
    int main()
    {
    	FILE *fin= NULL;
    	int i = 0, Numbers = 0, IntArray[100];
    
    	fin = fopen("numbers.txt", "rt");
    
    	while (fscanf(fin, "%d", &Numbers) != EOF)
    	{
    		IntArray[i++] = Numbers;
    	}
    	getch();
    	return 0;
    }
    Have phun!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    ok, thats pretty much the same thing i posted except i
    i used fgets, strtol, and getchar instead of fscanf and getch.

    you say you were showing him how to print out the numbers but his post wasnt asking for that. i still dont see how the efficiency of...

    Code:
    fscanf(file, "%d %*d %d %*d %d", &a, &b, &c);
    really matters in this case. i just answered his question. he wasnt asking how to print the numbers out or save them to an array of int.

  15. #15
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    you say you were showing him how to print out the numbers but his post wasnt asking for that.
    I know that but I just want to show him how to read numbers from a file and I didn't want to give him a direct solution.

    i still dont see how the efficiency of...
    Code:
    fscanf(file, "%d %*d %d %*d %d", &a, &b, &c);
    really matters in this case. i just answered his question
    And what about if you have 1000 of numbers in your file. What will you do?? Write 1000 variables for each number or you will be smart and saved them into an array of integers?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  5. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM