Connecting Tech Pros Worldwide Forums | Help | Site Map

ifstream and format issues

Pat
Guest
 
Posts: n/a
#1: Dec 29 '05
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[color=blue][color=green]
>> ws >> newDir >> ws >> newFile
>> ws >> newApp;[/color][/color]

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

Jim Langston
Guest
 
Posts: n/a
#2: Dec 29 '05

re: ifstream and format issues


"Pat" <patc6@cox.net> wrote in message
news:yvMsf.35799$Mi5.12465@dukeread07...[color=blue]
> 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[color=green][color=darkred]
> >> ws >> newDir >> ws >> newFile
> >> ws >> newApp;[/color][/color]
>
> 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[/color]

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.


Evan Carew
Guest
 
Posts: n/a
#3: Dec 30 '05

re: ifstream and format issues


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pat wrote:[color=blue]
> 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() )[/color]
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.[color=blue]
> {
> //Date Time Dir Size FileName
> inFile >> ws >> newDate >> ws >> newTime[color=green][color=darkred]
> >> ws >> newDir >> ws >> newFile
> >> ws >> newApp;[/color][/color]
>
> 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[/color]

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

iD8DBQFDtXfVpxCQXwV2bJARAn9EAJ9XGY3ifpnrCiCF0n5iVN UcClDhmwCfQ/wI
RO5uR2leDEWa4vKNpuX+uos=
=aBD+
-----END PGP SIGNATURE-----
Closed Thread