473,508 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error reading file IO in C

6 New Member
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. int main()
  6.  
  7.  
  8.  
  9. int choice, count;
  10. FILE *inputfile = NULL;
  11.  
  12. char option,pin,transaction;
  13. int deposit, withdraw,fpin,fbal,temp;
  14. option='0';
  15. int acctid[20], acctbal[20];
  16.  
  17. choice = 3;
  18. while(choice != 2)
  19. {
  20. system ("cls");
  21. printf("WELCOME TO SAMUEL'S BANK\n");
  22. printf("------------------------------\n\n");
  23. printf("Main Menu\n");
  24. printf("------------------------------\n");
  25. printf("1)Enter Pin #\n");
  26. printf("2)Exit\n");
  27. printf("------------------------------\n\n");
  28. scanf("%c", &option);
  29.  
  30. if (option =='2')
  31. {
  32. break;
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39. if (option == '1')
  40. {
  41. system ("cls");
  42. printf("Pin Number Verification\n");
  43. printf("------------------------------\n\n");
  44. printf("Please enter your pin number: ");
  45. scanf("%s", &pin);
  46. }
  47.  
  48.  
  49.  
  50.  
  51. inputfile =fopen("info.txt","r");
  52.  
  53. if (inputfile == NULL) 
  54.  
  55. {
  56. printf("Unable to open input file\n");
  57.  
  58. return 1;
  59. }
  60.  
  61. count = 0;
  62. while ( fscanf(inputfile, "%d" "%d", &fpin, &fbal ) == 2 )
  63. {
  64. count++;
  65. acctid[count] = fpin;
  66. acctbal[count] = fbal;
  67. if (acctid[count] == pin)
  68. {
  69.  
  70. temp = count;
  71.  
  72.  
  73.  
  74. system ("cls"); 
  75. printf("Transactions\n");
  76. printf("------------------------------\n");
  77. printf("1)Deposit\n2)Withdraw\n3)Statement\n4)Exit\n");
  78. printf("------------------------------\n");
  79. scanf("%s",&transaction);
  80.  
  81.  
  82.  
  83.  
  84.  
  85. if (transaction =='1')
  86.  
  87. {
  88. system ("cls");
  89. printf("Deposit\n");
  90. printf("------------------------------\n\n");
  91. printf("Please enter the amount of money you would like to deposit: ");
  92. scanf("%d", &deposit);
  93. }
  94.  
  95. if (transaction =='2')
  96.  
  97. {
  98. system ("cls");
  99. printf("Withdraw\n");
  100. printf("------------------------------\n\n");
  101. printf("Please enter the amount of money you would like to withdraw: ");
  102. scanf("%d", &withdraw);
  103. }
  104.  
  105. if (transaction=='3')
  106.  
  107. {
  108. system ("cls");
  109. printf("Statement\n");
  110. printf("------------------------------\n\n");
  111. }
  112.  
  113. fclose(inputfile);
  114.  
  115. }
  116.  
  117. }
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. return 0;
  139.  
  140.  
  141. }
  142.  

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


111 234
002 892
003 463
004 673
005 932
Dec 7 '08 #1
9 1807
Banfa
9,065 Recognized Expert Moderator Expert
what errors or output messages do you get?
Dec 7 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Dec 9 '08 #3
JosAH
11,448 Recognized Expert MVP
@weaknessforcats
Like this?

Expand|Select|Wrap|Line Numbers
  1. int acctid[20], acctbal[20];
  2.  
... yes you can in all versions of C you can; check it out.

kind regards,

Jos
Dec 9 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Dec 10 '08 #5
Tassos Souris
152 New Member
Actually you can do that in C:
Expand|Select|Wrap|Line Numbers
  1. int main( int argc, char **argv ){
  2.    int a;
  3.    ....
  4.    int b;
  5. }
  6.  
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...
Dec 11 '08 #6
Banfa
9,065 Recognized Expert Moderator Expert
@Tassos Souris
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.
Dec 12 '08 #7
Tassos Souris
152 New Member
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
Dec 12 '08 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
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...
Dec 12 '08 #9
Tassos Souris
152 New Member
@weaknessforcats
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!!!
Dec 12 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

20
7876
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
2
4047
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException...
0
4989
by: Rhon Stewart via DotNetMonster.com | last post by:
Hi please visit this link : http://www.eggheadcafe.com/articles/pfc/selfupdater.asp I followed all the steps for listed on the link , when I execute the application it it gives me the following...
25
17143
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from...
0
3574
by: Mart | last post by:
Hi, I have just written (my first) VB.net app using MS Visual Basic 2005 Express Edition Beta. It is fairly simple, it reads some configuration data from an XML file then opens a new window...
6
4144
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
1
3914
by: RADAR | last post by:
hi, the error code was taken from msdn and it occured in my code as follows; i am reading buffer from the file : #include <stdio.h> #include <stdlib.h> #define SIZE 10 #define DSIZE 5...
1
3002
by: Steve Ametjan | last post by:
I've been trying to get MySQL-python to install on Leopard for the past couple of days, and I keep running into relatively the same error. I'm hoping that someone on this list will be able to...
4
2133
by: radhikams | last post by:
hi I have written a code for writing the data of a column from database into a file and again reading that file and displaying. Im writing into an .html file....Now the problem is im...
0
1757
Guido Geurs
by: Guido Geurs | last post by:
I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files. When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.