9628657
asked on
help needed ( urgent )
I have the following code that reads in a .txt file and stores it into an array.
A typical .txt file format that I would want to read in is :
6
22533
17414
40266
2048
1
0
5
33
45
etc...
Now the little program I have reads the file and stores each line as characters ( I THINK ! ). This is no good since I can not perform any mathematical operation on the contents of the array.
Does anyone know how to modify the program so that it reads in the file and stores the contens as numbers in the array.
The code is
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define MAX_LINE 20
void main( void )
{
FILE *stream;
char line[MAX_LINE];
char *array[1000];
int nCount = 0;
const int ArSize = 12 ;
char file[ArSize] ;
cout<< "Enter file name to load please : " ;
cin >> file ;
if( (stream = fopen( file, "r" )) != NULL )
{
while ( fgets( line, MAX_LINE, stream ) != NULL)
{
array[nCount] = new char[strlen(line)+1];
strcpy(array[nCount], line);
nCount++;
}
fclose( stream );
}
printf("readed lines %d\n", nCount);
// Don't for get to delete all the Array elements before leaving!
cout << array[8] << "\n"
}
Cheers...
A typical .txt file format that I would want to read in is :
6
22533
17414
40266
2048
1
0
5
33
45
etc...
Now the little program I have reads the file and stores each line as characters ( I THINK ! ). This is no good since I can not perform any mathematical operation on the contents of the array.
Does anyone know how to modify the program so that it reads in the file and stores the contens as numbers in the array.
The code is
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define MAX_LINE 20
void main( void )
{
FILE *stream;
char line[MAX_LINE];
char *array[1000];
int nCount = 0;
const int ArSize = 12 ;
char file[ArSize] ;
cout<< "Enter file name to load please : " ;
cin >> file ;
if( (stream = fopen( file, "r" )) != NULL )
{
while ( fgets( line, MAX_LINE, stream ) != NULL)
{
array[nCount] = new char[strlen(line)+1];
strcpy(array[nCount], line);
nCount++;
}
fclose( stream );
}
printf("readed lines %d\n", nCount);
// Don't for get to delete all the Array elements before leaving!
cout << array[8] << "\n"
}
Cheers...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.