Error reading file IO in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • techking
    New Member
    • Nov 2008
    • 6

    Error reading file IO in C

    Hello ive written a program but it does not want to read from a referenced text file.
    i wonder if anyone can tell me what the problem is? below is the source code and after that is the text file.
    Code:
     
     
    #include<stdio.h>
    #include<stdlib.h>
    int main()
     
    { 
     
     
    int choice, count;
    FILE *inputfile = NULL;
     
    char option,pin,transaction;
    int deposit, withdraw,fpin,fbal,temp;
    option='0';
    int acctid[20], acctbal[20];
     
    choice = 3;
    while(choice != 2)
    {
    system ("cls");
    printf("WELCOME TO SAMUEL'S BANK\n");
    printf("------------------------------\n\n");
    printf("Main Menu\n");
    printf("------------------------------\n");
    printf("1)Enter Pin #\n");
    printf("2)Exit\n");
    printf("------------------------------\n\n");
    scanf("%c", &option);
     
    if (option =='2')
    {
    break;
    }
     
     
     
     
     
    if (option == '1')
    {
    system ("cls");
    printf("Pin Number Verification\n");
    printf("------------------------------\n\n");
    printf("Please enter your pin number: ");
    scanf("%s", &pin);
    }
     
     
     
     
    inputfile =fopen("info.txt","r");
     
    if (inputfile == NULL) 
     
    {
    printf("Unable to open input file\n");
     
    return 1;
    }
     
    count = 0;
    while ( fscanf(inputfile, "%d" "%d", &fpin, &fbal ) == 2 )
    {
    count++;
    acctid[count] = fpin;
    acctbal[count] = fbal;
    if (acctid[count] == pin)
    {
     
    temp = count;
     
     
     
    system ("cls"); 
    printf("Transactions\n");
    printf("------------------------------\n");
    printf("1)Deposit\n2)Withdraw\n3)Statement\n4)Exit\n");
    printf("------------------------------\n");
    scanf("%s",&transaction);
     
     
     
     
     
    if (transaction =='1')
     
    {
    system ("cls");
    printf("Deposit\n");
    printf("------------------------------\n\n");
    printf("Please enter the amount of money you would like to deposit: ");
    scanf("%d", &deposit);
    }
     
    if (transaction =='2')
     
    {
    system ("cls");
    printf("Withdraw\n");
    printf("------------------------------\n\n");
    printf("Please enter the amount of money you would like to withdraw: ");
    scanf("%d", &withdraw);
    }
     
    if (transaction=='3')
     
    {
    system ("cls");
    printf("Statement\n");
    printf("------------------------------\n\n");
    }
     
    fclose(inputfile);
     
    }
     
    }
     
     
    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    return 0;
     
     
    }

    ok here is the text file. It is titled info.txt.


    111 234
    002 892
    003 463
    004 673
    005 932
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    what errors or output messages do you get?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Learn how to use your debugger.

      First, this isn't C code. In C all variables must be defined at the top of the function. But you define two arrays after you say option='0';

      Second, you can't define the two arrays on the same line of code. At least you can't in C89 which I am using. Define the arrays on separate lines.

      Third, your pin is an it but in scanf it is used a %s which is a string. Yiou need to use %d or make pin an array of char.

      Fourth, you need to eat enter keys after the scanf's.

      Fifth, your count++ in side the while loop needs to be at then end of the loop rather than the begining. Otherwise, you fill your arrays starting in element 11 rather than element 0.

      Otherwise, the code appears to work.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by weaknessforcats
        Second, you can't define the two arrays on the same line of code. At least you can't in C89 which I am using. Define the arrays on separate lines.
        Like this?

        Code:
        int acctid[20], acctbal[20];
        ... yes you can in all versions of C you can; check it out.

        kind regards,

        Jos

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by JosAH
          yes you can in all versions of C you can; check it out.
          I rechecked this and you are correct as usual. I must have misinterpreted an error I was getting.

          Comment

          • Tassos Souris
            New Member
            • Aug 2008
            • 152

            #6
            Actually you can do that in C:
            Code:
            int main( int argc, char **argv ){
               int a;
               ....
               int b;
            }
            This is an addition to the language as part of the C99 Standard. There are some other features too..
            Of course Visual Studio is the only compiler (well not true but whatever.. :-) ) that does not support the C99 Standard...

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by Tassos Souris
              Of course Visual Studio is the only compiler (well not true but whatever.. :-) ) that does not support the C99 Standard...
              The is extremely in accurate (although I realise you realise that), in my experience the vast majority of C compilers are C89 compilers and there are few C compilers that fully support C99. gcc comes very close but is not complete (Status of C99 features in GCC 4.0 - GNU Project - Free Software Foundation (FSF)).

              My theory is that around the time that C99 was release C++ was already established. Once you have a good compliant C++ compiler for a platform there is very little need for a C compiler. There is nothing you can do with C that you can't do with C++ so development of C compilers on these platforms grinds to a halt.

              This left platforms which did not have C++ compilers. As far as I can tell these tend to be small microprocessors (although I am sure some clever clogs will come up with an exception to that). These are processors for hardware geeks not software geeks, these guys don't really care about the additional features of C99, in my experience they don't use the advanced features of C89 (you know like pointers) if they can avoid it so those platforms compilers don't receive a lot of development either.

              The result is that although C99 is nearly 10 years old it really doesn't appear to have had a large take-up.

              It is certainly true that on all the platforms I have worked on (around 10) if there has not been a C++ compiler the C compiler has been a C89 compiler.

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #8
                I agree with the above (as you understood) but C++ standard is screaming out for compliance with the C standard.. as C++'s creator said....
                so C99 will be supported by all compilers :P

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by Tassos Souris
                  but C++ standard is screaming out for compliance with the C standard..
                  Interesting comment since C++ doesn't work like C in all places. Like, typecasting, const, bitfields, function structure, longjmp, etc...

                  Comment

                  • Tassos Souris
                    New Member
                    • Aug 2008
                    • 152

                    #10
                    Originally posted by weaknessforcats
                    Interesting comment since C++ doesn't work like C in all places. Like, typecasting, const, bitfields, function structure, longjmp, etc...
                    It isn't something that i said; as i wrote in my post "the c++'s creator" said that...
                    I'm 99% sure of that... if i am wrong then my mistake... sorry!
                    Whatever... even though the languages differ (and that is more than logical!!!! :-) )
                    C++ "thinks" of C.. :-) :-)

                    Well, enough of that topic!! We are getting out of scope now!!!

                    Comment

                    Working...