I stream in a text file, which uses semi colons to separate values. I want to replace the semi colon with a colon, and stream out into a newly made .csv file
CURRENTLY, the program runs without an error during compiling, but when i specify an output filename, it creates the .csv file then crashes.
I can not upload images in my post because I am a new member...
Here is the Visual Studio Output When I compile:
BEGIN
1>------ Build started: Project: export, Configuration: Release Win32 ------
1> export.cpp
1> Generating code
1> Finished generating code
1> export.vcxproj -> J:\Bar\OIL Final\export\Release\export.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
END
Here is the 'guts' of the code:
string file_type = ".csv"; // Stores the Export
Filename Extension string file_path = ""; // Stores the
Export Filename and Path string filename;
bool validateExportFilename()
{
bool valid_filename = false; // Holds the Bool Value which
maintains a loop until vaid file is found
while (valid_filename == false)
{
std::cout << "\t\tPlease Enter A Name For These Results:\t";
cin >> filename;
file_path = export_folder + filename + file_type;
ifstream check_filename; // Define the Input
File Stream --> using USER entered file name check_filename.open(file_path); // Open the file fresh - all
positions in memory (i.e. getline() location) will be reset.
if (check_filename.fail())
{
t("FILE NAME IS VALID..."); // TRACE MESSAGE
valid_filename = true;
}
else
{
std::cout << "\n\t\t\t[!] ERROR - File Exists. Try again. [!]\n" << endl; //
Notify USER of Error std::cout << "\n\n\t\t\t";
// Format the pause command's screen output
system("pause");
// Pause to give user the time to find the file
}
check_filename.close();
}
return 0;
}
if (inFile.is_open())
{
while (!inFile.eof())
{
string line = "";
getline(inFile, line);
line.replace(line.begin(), line.end(), ";", ", ");
outFile << line << endl;
}
}
t("End Function - Replace"); // TRACE MESSAGE
inFile.close();
outFile.close();
return 0;
Here is a sample of the data of the text file, i am trying to stream in, and turn into a CSV file:
4/09/2013;10:34:57 AM;4780;1129;63;32;19.9;17.7;13.5;12.6;OK;0;50;4.77;26; 144
4/09/2013;10:35:11 AM;4546;1117;69;30;19.8;17.7;13.7;12.5;OK;0;50;4.76;26; 143
And here is what i want to stream out to a different file, but with the extention .CSV instead of .TXT:
4/09/2013,10:34:57 AM,4780,1129,63,32,19.9,17.7,13.5,12.6,OK,0,50,4.77,26, 144
4/09/2013,10:35:11 AM,4546,1117,69,30,19.8,17.7,13.7,12.5,OK,0,50,4.76,26, 143
And here is what the debugger outputs when the program crashes:
The thread 0x1d5c has exited with code -1073741510 (0xc000013a).
The program '[10012] export.exe' has exited with code -1073741510 (0xc000013a).
Unhandled exception at 0x637B7666 (msvcr120.dll) in export.exe: Fatal program exit requested.
Call stack lists this:
msvcr120.dll!637b7666() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for msvcr120.dll]
[External Code]
> export.exe!replaceAndExport() Line 204 C++
export.exe!wmain() Line 79 C++
And under Autos:
- inFile {_Filebuffer={_Set_eback=0x00000000 <NULL> _Set_egptr=0x00000000 <NULL>
_Pcvt=0x00000000 ...} } std::basic_ifstream > + std::basic_istream > {_Chcount=0 }
std::basic_istream > + std::basic_ios > {_Mystrbuf=0x001efb88 {_Set_eback=0x00000000
_Set_egptr=0x00000000 _Pcvt=0x00000000 ...} ...}
std::basic_ios > + _Filebuffer {_Set_eback=0x00000000 _Set_egptr=0x00000000 _Pcvt=0x00000000
...} std::basic_filebuf > - line "" std::basic_string,std::allocator > [size] 0 unsigned int [capacity] 15 unsigned int + [Raw View] 0x001efcd8 {...}
std::basic_string,std::allocator > * - outFile {_Filebuffer={_Set_eback=0x00000000 _Set_egptr=0x00000000
_Pcvt=0x00000000 ...} } std::basic_ofstream > + std::basic_ostream > {...}
std::basic_ostream > + std::basic_ios > {_Mystrbuf=0x001efc34 {_Set_eback=0x00000000
_Set_egptr=0x00000000 _Pcvt=0x00000000 ...} ...}
std::basic_ios > + _Filebuffer {_Set_eback=0x00000000 _Set_egptr=0x00000000 _Pcvt=0x00000000
...} std::basic_filebuf >
I can see the .CSV file being created, but the file remains empty at 0KB and a split second after the file is
created, the application crashes.
I feel like i may need to adjust the "read a line, swap commas in" process so i dont overflow the buffer, but
i am not a programmer so i dont really know...
As you've pointed out, the problem lies with the line line.replace(line.begin(), line.end(), ";", ", ");
Since you seem determined to emphasize on the fact that you're not a programmer, I will not go into detail as to why it compiles even though you're using it wrong. Instead, I will explain how this function is supposed to be used. While they are a number of variations to this function, the main purpose of string::replace
is to replace a portion of one string
with another string
.
Simply put, I can use string::replace
to change Hello World
to Goodbye Cruel World
by stating that I want Hello
to be replaced with Goodbye Cruel
. This, of course, works for not just the beginning but also any part of the string.
So that's not the function you want.
The function you want to use is present in <algorithm>
called replace
. replace
is similar to string::replace
except that instead of replacing a range, you specify the old value (in this case ';'
) and the new value (in this case ':') and you pass them into the function like this, replace(line.begin(), line.end(), ';', ':');
. Now what replace
will do is search through the range from line.begin()
to line.end()
for all instances of ';'
and replace them with ':'
.
Thank you for reading.
User contributions licensed under CC BY-SA 3.0