I am still having difficulty with this read file thing. I tried all the suggestions in this thread, but nothing seems to work. So, I'll try explaining the problem again:
What I am trying to do is read four variables per line from a text file that was exported from Access. Each line contains a char (conductor), an integer (line), and two floats (number1 & number2). The code that I have written below seems to read the data, but the output is meaningless, unformatted data. Instead of getting the conductor name out of the file (which should be "8-28.17"), I get a bunch of characters that mean nothing, followed by a long negative integer and two very large float numbers; all of which are not equal to what is written in the text file. I tried removing the function "read" as suggested above, but the results were the same. I am open to any suggestions that you may have, since I seem to be spinning my wheels on this one.
It compiles fine, but when I run the code, I get the following error:
Code:
'Read File.exe': Loaded 'C:\WINNT\system32\NTDLL.DLL', Cannot find or open a required DBG file.
'Read File.exe': Loaded 'C:\WINNT\system32\KERNEL32.DLL', Cannot find or open a required DBG file.
The program '[1252] Read File.exe: Native' has exited with code 0 (0x0).
So, as it stands, here is the code that I am using (but is not working yet):
Code:
// Reading data from a text file exported from Access
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <stdlib.h>
#include "read.h"
void outputLine( const FileData & );
int main()
{
// ifstream constructor opens the file
ifstream Conductor( "tblConductor.txt", ios::in );
if ( ! Conductor.is_open() ) {
cerr << "ERROR - File could not be opened\n";
exit(1);
}
cout << setiosflags( ios::left ) << setw( 12 ) << "Conductor" << setw( 12 ) << "Line"
<< setw( 12 ) << "Number1" << setw( 20 ) << "Number2" << endl;
FileData file;
Conductor.read( reinterpret_cast<char*>( &file ), sizeof ( FileData ) );
while ( Conductor ) {
outputLine( file );
Conductor.read( reinterpret_cast<char *>( &file ), sizeof( FileData ) );
}
return 0; // ifstream constructor closes the file
}
void outputLine( const FileData &f )
{
cout << setiosflags( ios::left ) << setw( 12 ) << f.cond << setw( 12 ) << f.line
<< setw( 12 ) << f.number1 << setw( 20 ) << f.number2 << '\n';
}
Here is the content of the text file for those interested:
Code:
"#2" 0 130.00 170.00
"#2ACSR" 0 160.00 230.00
Here is my "read.h" file:
Code:
// Header file for ReadFile.cpp
#ifndef READ_H
#define READ_H
struct FileData {
int line;
char cond [ 30 ];
float number1, number2;
};
#endif
Also, one other question, could the size of my cond array above be the problem?
Thanks!!