www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/04/06/16:50:44

From: "Mr. Veli Suorsa" <VJSuorsa AT Surfeu DOT Fi>
Sender: vjsuorsa AT Surfeu DOT Fi
To: Djgpp AT delorie DOT com
Cc: VJSuorsa AT Surfeu DOT Fi
Date: Fri, 6 Apr 2001 23:50:17 +0300
Subject: Read with C++ <fstream.h>
X-Mailer: CWMail Web to Mail Gateway 2.6k, http://netwinsite.com/top_mail.htm
Message-id: <3ace2c09.4fbe.0@surfeu.fi>
X-User-Info: 193.167.106.194
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019