reading from a file... fgetc??

This is a discussion on reading from a file... fgetc?? within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to do something that is easy.. .but it doesn't seem to be working right. From the command ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Athens, Greece
    Posts
    5

    reading from a file... fgetc??

    Hi,

    I'm trying to do something that is easy.. .but it doesn't seem to be working right.

    From the command prompt.. the user should enter the name of the program to run followed by a .txt file. The .txt file has in it two numbers.. let's say 5 77.

    I want those numbers to be saved in my integer variables numberA and numberB respectively.

    here is what I have so far.. but it doesn't work...

    Code:
    int main (int argument1,char*argumentx[])
    {
        int numberA,numberB,c;
        FILE *myfile;
        
        myfile = fopen(argumentx[1],"r");
        
        if (argument1 != 2)
        {
         printf("Error: you have input too many arguments. Try again.");
        }
        
        if (myfile == NULL)
        {
         printf("Error: Invalid filename supplied (%s)\n",argumentx[1]);
        }
        
        while ((c = fgetc(myfile)) != EOF)
        {
          numberA = fgetc(myfile);
          numberB = fgetc(myfile);
        printf("the numberA has a value of %d\n",numberA);
        printf("the numberB has a value of %d\n",numberB);
        }

    the printf does not even print the correct numbers.
    Any suggestions?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    yassas!

    is this the full program? where is the prompt for the information so i can run it to see?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Athens, Greece
    Posts
    5
    Well, the rest of it doesn't really work since I've added this.

    If you save this program, you can run it. Say you save it saved as project1.cpp in C:\homework

    also, open notepad and type in two numbers... let's say 10 30 do
    save as and save it as file.txt

    when you get into the command prompt type cd.. till you get
    C:\>
    Then Type cd homework
    and it should show this:
    C:\HOMEWORK>

    then type: project1 file.txt

    and it should run!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgetc reads one character. So in your sample, the first read will read the 5 and the second call will read the space. Consider using another method, such as the combination of fgets and sscanf. The FAQ covers reading input in a variety of methods.


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

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Not that it's a problem but I noticed you used 'argument1' for argc and 'argumentx' for argv... why?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The arguments to main can be called anything you like, so long as it's legal for a variable name.


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

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Quote Originally Posted by demi222
    If you save this program, you can run it. Say you save it saved as project1.cpp in C:\homework
    If you are writing C code, it should be .c instead of .cpp. If you are not writing C code, you're in the wrong forum. :P

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Athens, Greece
    Posts
    5
    So in other words, trash it and start over... right?

    and by the way.. I am writing in C... my compiler saves it as .cpp!!!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    What do you think "save as..." means?

    > myfile = fopen(argumentx[1],"r");
    Check the count of arguments (I hate your choice of names - everyone knows what argc and argv mean, yet your names aren't even consistent) before you try and use the argv.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Location
    Athens, Greece
    Posts
    5
    why are you all so mean? I just asked for some help.

    I changed the arguments to argc and argv.

    And I don't know they have a meaning.
    I am trying to learn C on my own.. (distance learning) and am trying to make this program work.

    Anyways...

    thanks for your help!

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here, the problem is, as quzah has already stated, is that you're not using the right function. fgetc gets 1 CHARACTER from a file, not an integer.

    Let me explain more in detail.

    ********** Your file ************************
    5 77
    ******************************************

    5 is character 1.
    (space) is character 2.
    7 is character 3, and so on.

    When your program is executed, the first fgetc (c=fgetc(myfile)) gets the first character of my file and places it in c. So c = '5' (or, in fact, its ASCII integer value, since c is an int). Next, you do another fgetc, and that puts (space) into numberA. Then another one puts '7' into numberB.

    Thus, you output, will likely be something like

    NumberA :
    NumberB : 7

    This can be remedied by using fgets or sscanf, and for that you should read the FAQ on the site.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Registered User
    Join Date
    Oct 2005
    Location
    Athens, Greece
    Posts
    5
    what gets saved in num1 is 5 25. I don't want that. I want
    num1 to have 5 and num2 to have 25. How do I separate these?
    I am reading the whole line... how do I find where the space is so
    that I know it is the different number.

    can anyone help?

    Code:
    FILE *myfile;    /* internal name for the file */
        char num1[5],num2[5];
        
        if ((myfile = fopen(argv[1],"r"))==NULL)
        {
         fprintf(stderr,"The file could not be opened.  Invalid name: %s",argv);
         exit(-1);
         }
    
    while(!feof(myfile))
        {
         fgets(num1,5,myfile);
         counter++;
         fgets(num2,5,myfile);
         counter++;
         printf("num1 is: %s",num1);
          printf("num2 is: %s",num2);
        }
        fclose(myfile);

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use fgets and sscanf, as previously suggested.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[128];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             int num1, num2;
             if ( sscanf(line, "%d %d", &num1, &num2) == 2 )
             {
                printf("num1 = %d, num2 = %d\n", num1, num2);
             }
          }
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    num1 = 5, num2 = 77
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    and by the way.. I am writing in C... my compiler saves it as .cpp!!!
    If you're using dev-c++, just add .c to the end and it will save it as such. Or choose C Source Files from the drop down menu of file types.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21