473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

fscanf not working

1
When i am writing a simple c program-write to a file and read from it,fscanf not working.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     FILE *ptr,*ptr1;
  5.     int s,r;
  6.     clrscr();
  7.     ptr=fopen("myfile.txt","w");
  8.     do
  9.     {
  10.     scanf("%d",&s);
  11.     fprintf(ptr,"%d",s);
  12.     }while(!feof(stdin));
  13.     fclose(ptr);
  14.     ptr1=fopen("myfile.txt","r");
  15.         while(!feof(ptr1))
  16.     {       
  17.         fscanf(ptr1,"%d",&r);
  18.  
  19.         printf("%d\n",r) ;
  20.                 getch();
  21.     }
  22.     fclose(ptr1);
  23.     getch();
  24.     return 0;
  25. }
Please help me to find out the error
Jul 2 '07 #1
2 2902
sicarie
4,677 Expert Mod 4TB
What errors are you getting, besides those not seeing 'getch()' (which is in conio.h, which you dont' include, and shouldn't, but also shouldn't be using getch..., as a personal aside). I would imagine this isn't compiling yet, right?
Jul 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Various things not correct. Here is a working version:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. int main()
  5. {
  6. FILE *ptr,*ptr1;
  7. int s,r;
  8. system("cls");
  9. //clrscr();
  10. ptr=fopen("C:\\scratch\\instructor\\myfile.txt","w");
  11. while (scanf("%d",&s) != EOF)
  12. {
  13.     fprintf(ptr,"%d ",s);
  14. }
  15. //do
  16. //{
  17. //scanf("%d",&s);
  18. //fprintf(ptr,"%d",s);
  19. //}while(!feof(stdin));
  20.  
  21. fclose(ptr);
  22. ptr1=fopen("myfile.txt","r");
  23. while (fscanf(ptr1,"%d",&r) != EOF)
  24. {
  25.     printf("%d\n",r) ;
  26. }
  27. //while(!feof(ptr1))
  28. //{ 
  29. //fscanf(ptr1,"%d",&r);
  30.  
  31. //printf("%d\n",r) ;
  32. //getch();
  33. //}
  34. fclose(ptr1);
  35. getch();
  36. return 0;
  37. }
  38.  
I did a check for EOF rather than use feof(). You have to check that your scanf() and fscanf() actaully worked.

EOF is CTRL+Z and marks the end of the data stream.

I added a space after the int in fprintf() so that fscanf() could see the individual int values. If you don't do this and write values of 1 2 3, in the disc file will be 123 and that will come back as one int.
Jul 2 '07 #3

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

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
6
by: InuY4sha | last post by:
Hi, I hope to be not off topic.. I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC address) I use fscanf like this fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X", ptr, ptr+1, ptr+2,...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
3
by: Shisou | last post by:
Hey bytes community! this one is a really strange issue I ran into and I'm hoping you all can shine some light on it for me. This is written in C, not C++ I'm working on a program that reads...
42
by: Bill Cunningham | last post by:
I'm doing something wrong and all I know to do is turn to clc. I have a text file containing 2 doubles separated by a tab. ..26 0 Is the text. I want to read the two double and printf them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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,...

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.