ifstream and format issues

P

Pat

Here is my problem that I am having with a current project. I went and
backed up all my hard drives to DVD's. After backing up the drives, I
used a batch file to get all the file names from the disk to a text file
for future reference. I now have over 60 huge text files and would like
to place the information into either a database or an excel document.

I would like to read in from a text file and output the text file to an
excel .cvs file or insert statements for an .sql file. The text files
are in a strange format and here is an example:

12/27/2005..12:34.AM....<DIR>..........Mandrakelinux
07/07/2005..03:05.AM.......726,444,032.Mandrakelinux.10.1.iso
07/07/2005..03:05.AM.......728,047,616.Mandrakelinux.10.1.iso
( The '.' are whitespaces and all the files are similar to this )

I have the output finished for either format, but getting the correct
format read in is another beast.

//Yes I know Formadder is spelled wrong I just don't like the term
//format when using a windows machine. HA HA.
void Formadder::getData()
{
string newDate; ////
string newTime; // Values for local storage
string newDir; // to put into a vector of
string newFile; // undetermined size.
string newApp; ////

counter = 0; // Used for the application number later.

// Just asking the user for the file name
cout << "Please enter a file name to open: ";
cin >> userInput;
cout << "\n";

//Opening the file and checking to see if the
//file is there.
ifstream inFile;
inFile.open( userInput.c_str() );
if( inFile.fail() )
{
throw( "Could not locate or open file." );
}

//The loop is for getting the contents of the file and
//placing the contents into a vector.
while( !inFile.eof() )
{
//Date Time Dir Size FileName
inFile >> ws >> newDate >> ws >> newTime
date.push_back( newDate );
time.push_back( newTime );
directory.push_back( newDir );
fileSize.push_back( newFile );
appName.push_back( newApp );
counter += 1;
}
inFile.close(); //Closing file
}// End function getData

The problem is dealing with the output and whitespaces. ifsteam appears,
and according to several sources, to look for whitespaces or tabs as new
items. This throws off the format that I am trying to achieve. Here is
an example format:

Please enter a file name to open: test.txt

Please enter the disk number: 23

23 1 12/23/2005 01:15 AM <DIR> alg
23 2 12/23/2005 01:15 AM <DIR> ARCADE
23 3 12/23/2005 01:15 AM <DIR> Backup
23 4 12/23/2005 01:17 AM <DIR> Civ4
23 5 12/23/2005 01:17 AM <DIR> CloneCD
23 6 v5.0.0.0 Final Incl 12/23/2005 01:18
23 7 AM <DIR> CombatFS 12/23/2005 10:36
23 8 AM <DIR> Conexant_Drivers 12/23/2005 01:18
23 9 AM <DIR> EBooks 12/23/2005 10:36


I hope that someone can help me to deal with the whitespaces. I have
managed to eliminate the starting whitespaces, but not the ones within
the time and file name.
This is the format I would like to get:
DiskNumber AppNumber Date Time Directory FileSize AppName

Thank you,
Patrick Conlon
 
J

Jim Langston

Pat said:
Here is my problem that I am having with a current project. I went and
backed up all my hard drives to DVD's. After backing up the drives, I used
a batch file to get all the file names from the disk to a text file for
future reference. I now have over 60 huge text files and would like to
place the information into either a database or an excel document.

I would like to read in from a text file and output the text file to an
excel .cvs file or insert statements for an .sql file. The text files are
in a strange format and here is an example:

12/27/2005..12:34.AM....<DIR>..........Mandrakelinux
07/07/2005..03:05.AM.......726,444,032.Mandrakelinux.10.1.iso
07/07/2005..03:05.AM.......728,047,616.Mandrakelinux.10.1.iso
( The '.' are whitespaces and all the files are similar to this )

I have the output finished for either format, but getting the correct
format read in is another beast.

//Yes I know Formadder is spelled wrong I just don't like the term
//format when using a windows machine. HA HA.
void Formadder::getData()
{
string newDate; ////
string newTime; // Values for local storage
string newDir; // to put into a vector of
string newFile; // undetermined size.
string newApp; ////

counter = 0; // Used for the application number later.

// Just asking the user for the file name
cout << "Please enter a file name to open: ";
cin >> userInput;
cout << "\n";

//Opening the file and checking to see if the
//file is there.
ifstream inFile;
inFile.open( userInput.c_str() );
if( inFile.fail() )
{
throw( "Could not locate or open file." );
}

//The loop is for getting the contents of the file and
//placing the contents into a vector.
while( !inFile.eof() )
{
//Date Time Dir Size FileName
inFile >> ws >> newDate >> ws >> newTime

date.push_back( newDate );
time.push_back( newTime );
directory.push_back( newDir );
fileSize.push_back( newFile );
appName.push_back( newApp );
counter += 1;
}
inFile.close(); //Closing file
}// End function getData

The problem is dealing with the output and whitespaces. ifsteam appears,
and according to several sources, to look for whitespaces or tabs as new
items. This throws off the format that I am trying to achieve. Here is an
example format:

Please enter a file name to open: test.txt

Please enter the disk number: 23

23 1 12/23/2005 01:15 AM <DIR> alg
23 2 12/23/2005 01:15 AM <DIR> ARCADE
23 3 12/23/2005 01:15 AM <DIR> Backup
23 4 12/23/2005 01:17 AM <DIR> Civ4
23 5 12/23/2005 01:17 AM <DIR> CloneCD
23 6 v5.0.0.0 Final Incl 12/23/2005 01:18
23 7 AM <DIR> CombatFS 12/23/2005 10:36
23 8 AM <DIR> Conexant_Drivers 12/23/2005 01:18
23 9 AM <DIR> EBooks 12/23/2005 10:36


I hope that someone can help me to deal with the whitespaces. I have
managed to eliminate the starting whitespaces, but not the ones within the
time and file name.
This is the format I would like to get:
DiskNumber AppNumber Date Time Directory FileSize AppName

Thank you,
Patrick Conlon

Consider reading an entire line at a time then parsing it. Your problem
seems to be in spaces in the file name, but since it's at the end of the
line, you should just take everything from the filename to the end of the
line as the filename.

Using >> you're not going to be able to do this I don't think, unless you,
somehow, check for \n which I'm not even sure >> can do for you.
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Here is my problem that I am having with a current project. I went and
backed up all my hard drives to DVD's. After backing up the drives, I
used a batch file to get all the file names from the disk to a text file
for future reference. I now have over 60 huge text files and would like
to place the information into either a database or an excel document.

I would like to read in from a text file and output the text file to an
excel .cvs file or insert statements for an .sql file. The text files
are in a strange format and here is an example:

12/27/2005..12:34.AM....<DIR>..........Mandrakelinux
07/07/2005..03:05.AM.......726,444,032.Mandrakelinux.10.1.iso
07/07/2005..03:05.AM.......728,047,616.Mandrakelinux.10.1.iso
( The '.' are whitespaces and all the files are similar to this )

I have the output finished for either format, but getting the correct
format read in is another beast.

//Yes I know Formadder is spelled wrong I just don't like the term
//format when using a windows machine. HA HA.
void Formadder::getData()
{
string newDate; ////
string newTime; // Values for local storage
string newDir; // to put into a vector of
string newFile; // undetermined size.
string newApp; ////

counter = 0; // Used for the application number later.

// Just asking the user for the file name
cout << "Please enter a file name to open: ";
cin >> userInput;
cout << "\n";

//Opening the file and checking to see if the
//file is there.
ifstream inFile;
inFile.open( userInput.c_str() );
if( inFile.fail() )
{
throw( "Could not locate or open file." );
}

//The loop is for getting the contents of the file and
//placing the contents into a vector.
while( !inFile.eof() )
Typically, the extractor mechanisms for formatted extraction are not
used on the stream. Instead, you often see something like the following:

while (infile.getline(line, BUFSIZE, '\n')) {

Then for the parsing operation, you might try and cleanup the input with
a mutating function such as remove to get rid of tabs.
{
//Date Time Dir Size FileName
inFile >> ws >> newDate >> ws >> newTime

date.push_back( newDate );
time.push_back( newTime );
directory.push_back( newDir );
fileSize.push_back( newFile );
appName.push_back( newApp );
counter += 1;
}
inFile.close(); //Closing file
}// End function getData

The problem is dealing with the output and whitespaces. ifsteam appears,
and according to several sources, to look for whitespaces or tabs as new
items. This throws off the format that I am trying to achieve. Here is
an example format:

Please enter a file name to open: test.txt

Please enter the disk number: 23

23 1 12/23/2005 01:15 AM <DIR> alg
23 2 12/23/2005 01:15 AM <DIR> ARCADE
23 3 12/23/2005 01:15 AM <DIR> Backup
23 4 12/23/2005 01:17 AM <DIR> Civ4
23 5 12/23/2005 01:17 AM <DIR> CloneCD
23 6 v5.0.0.0 Final Incl 12/23/2005 01:18
23 7 AM <DIR> CombatFS 12/23/2005 10:36
23 8 AM <DIR> Conexant_Drivers 12/23/2005 01:18
23 9 AM <DIR> EBooks 12/23/2005 10:36


I hope that someone can help me to deal with the whitespaces. I have
managed to eliminate the starting whitespaces, but not the ones within
the time and file name.
This is the format I would like to get:
DiskNumber AppNumber Date Time Directory FileSize AppName

Thank you,
Patrick Conlon

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFDtXfVpxCQXwV2bJARAn9EAJ9XGY3ifpnrCiCF0n5iVNUcClDhmwCfQ/wI
RO5uR2leDEWa4vKNpuX+uos=
=aBD+
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top