View Single Post
Old May 8th, 2007, 9:00 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 47
Rep Power: 0 csrocker101 is on a distinguished road
problem processing file into a char array

Hey I am back yet again with another dillema. This time I am trying to pass information from a file into a char array. I am able to do this succesfully with it properly formatted, but the problem is I need to use this char array in another class. Basically my whole problem has to do with my declaration of the char array. If declare the char array in my header file with the correct index size and then declare
line[128]
in my ReadFiles.cpp file, it only prints out the last line. Here is my code for the ReadFiles.cpp:
void readFiles::readFileInformation()
{
  char filename[] = "C:\\HighScores.txt";
 // char line[128];
  FILE *file = fopen ( filename, "r" );
 
 //determines if file can be opened 
 if ( file != NULL )
  {
	  //char array declared in header file
               line[128];
	  //read file with proper formatting
	  while ( fgets ( line, sizeof line, file ) != NULL )
	  {
		char *newline = strchr ( line, '\n' );
	    if ( newline != NULL )
		*newline = '\n';
	  }
	  fclose ( file );
  }
  else
  {
    perror ( filename );
  }
}

And here is my header file:

#pragma once
#include <string>
#include <fstream>

class readFiles
{
public:
	readFiles();
	~readFiles();
	void readFileInformation();
	char line[128];
};

It works...but only reads the last line. However when I take out the char array in the header file and in the ReadFiles.cpp put
if ( file != NULL )
  {
	  char line[128];

instead of line[128]. It works, and reads the file correctly from top to bottom properly formatted. So basically if I do "line[128]" it only prints out the last row of the file however; if I put "char line[128]" it prints out everything but I cant use the array in the other classes. But if I do this I cannot use the array in another class unless its in the header file. If declare both "char array[125]" in the header file and in the .cpp fie, the array is empty. I need top use this array in a "renderBitMapString" object in another class that prints the file out on the openGL window. Any ideas on how to fix this?
csrocker101 is offline   Reply With Quote