RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Read integers from a file

Join Date: Aug 2004
Posts: 5
Reputation: mrb260478 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mrb260478 mrb260478 is offline Offline
Newbie Poster

Read integers from a file

  #1  
Nov 5th, 2007
Can some one help me to :

How to Read integers stored in a text file in the following format :

26 52 23 41 19 61 54 4 19 95 84 25 55 22
(integers delimted by a space.)

to be used for sorting later.

Have tried following code:

#include <stdio.h>
#include <conio.h>

void main()
{
FILE *fp;
int *x,i=0;
int a,b;

fp=fopen("file1.txt","r");
if (fp==NULL)
{
printf("File Cannot be opened.\n");
}
fseek(fp,0L,2);
a=ftell(fp);
rewind(fp);
fseek(fp,+1,0);
b=ftell(fp);
while (b<a)
{
fscanf(fp," %d ",x[i]);
printf ("%2d - %d\n",i,x[i]);
++i;
b=ftell(fp);
//printf("ftell = %d y = %d\n",b,a);
}
fclose(fp);


}

Output (which is wrong, which is obvious):

0 - 1735
1 - 11552
2 - 12576
3 - 13111
4 - 2613
5 - 12576
6 - 11552
7 - 12576
8 - 13617
9 - 12853
10 - 8202
11 - 8242
12 - 8237
13 - 12849

Please help.

Manish
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:09 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC