Mail Archives: djgpp/2001/04/06/16:50:44
Hi!
Can You help me to convert this ascii- file reader (tested C program) to Gnu
C++ using <fstream.h> library?
And how do you put these read lines to an array ( lines[MAXLINELEN][ROW] )?
---
/* Program read lines from file.
(strread.c), Veli Suorsa, 06.04.2001 */
#include <stdio.h>
#define MAXLINELEN 110
int main( int argc, char *argv[] )
{
char filename[30], strline[MAXLINELEN];
int line = 0;
FILE *fileptr;
// Test if given parameter
if ( argc != 2 )
{
printf( "\nGive data file name : " );
gets( filename );
}
else
{
// Copy parameter to filename
strcpy( filename, argv[1] );
}
// Test if can read file
if ( ( fileptr = fopen( filename, "r" ) ) == NULL )
{
printf( "\nError: File: %s can't read.\n", filename );
exit(0);
}
// List file
printf( "\nFile %s list :\n", filename );
// Read and list, while not end of file
while ( !feof( fileptr ))
{
fgets( strline, MAXLINELEN, fileptr);
printf( "%s", strline);
// Line counter
line++;
}
// Close file
fclose( fileptr );
printf( "\n*** Read: %d rows from file: %s.\n", line, filename );
return 0;
}
---
Thanks in advance!
Veli Suorsa
---
"People must believe to the future to be able to live!"
---------------------------------- J.V.Snellman, 1890.
Oulu, FINLAND
Mailto:VJSuorsa AT Surfeu DOT Fi
http://members.surfeu.fi/veli.suorsa/
http://www.surfeu.fi
- Raw text -