![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2006
Posts: 41
Rep Power: 0
![]() |
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] 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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The statement, line [128], in your .cpp file is not a syntactically correct declaration. Just toss it. Further, sizeof line does not get you the size of the array; it gets you the size of a pointer to line.
If you want to use the array in several places, it has to be accessible in those places. This means you need to pass its location (where ever you decide to put it) around to where you need it. Your treatment regarding the newline character is just a bunch of logical waste. Read the docs for fgets. If you get a newline, it will be the last character (find that with strlen). No point setting it to a newline if it's already there. You might want to get rid of it by dumping a '\0' over it. If you want to add one, you'll need to make the buffer one byte longer than the length you ask for.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| Problem with file and array | Marek | C | 9 | Dec 28th, 2005 10:03 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |