•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,630 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2028 | Replies: 1
![]() |
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
Rule 1) A pointer isn't an array. Just because you say int *x doesn't mean you can immediately use x as if it were an array. You need to allocate memory to it first.
Rule 2) main returns int.
Rule 3) Don't try to use the file positioning functions until you actually know what you're doing. Just read from the file and everything will work.
It's really as simple as this:
If you want the list to grow indefinitely to meet the contents of the file, you have no choice but to allocate and resize the memory for a pointer manually (assuming you really are using C even though you posted in the C++ forum).
Rule 2) main returns int.
Rule 3) Don't try to use the file positioning functions until you actually know what you're doing. Just read from the file and everything will work.
It's really as simple as this:
c Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { FILE *in; int list[50]; int i; in = fopen ( "file1.txt", "r" ); if ( in != NULL ) { for ( i = 0; i < 50; i++ ) { if ( fscanf ( in, "%d", list[i] ) != 1 ) break; } for ( j = 0; j < i; j++ ) printf ( "%d - %d\n", j, list[j] ); fclose ( in ); } return 0; }
I'm here to prove you wrong.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Read XML File in C/C++ (C++)
- read each line of file (C)
- using a "for" loop to read a text file (VB.NET)
- read data from file (C++)
- Read text file to a certain point (C#)
- Using data i read from *.* files (C++)
- read a dat file through C (C)
Other Threads in the C++ Forum
- Previous Thread: Operating Systems
- Next Thread: Need help reading a file



Linear Mode