Just as a little test, I made a simple program that reads from a .txt file each line, and then stores whatever its between quotes on that line on a temporary string.
Then, it writes every character on the line to another string until it finds the ending quote.
Everything "seems" to go fine, as the compiler doesn't throw any errors, but whenever I run the program, after reading the lines, when it tries to output the lines to the screen, it instead shows a bunch of garbage.
Here's the program
Code:
#include <iostream>
#include <fstream>
#include <string>
#define LINES 20
#define MAXIMUMCHAR 30
using namespace std;
int main(){
bool progfailed = false;
ifstream archivo;
archivo.open("probando.txt");
string temporal;
string *lines = new string[LINES];
if(archivo.is_open()){
while(!(archivo.eof())){
for(int i = 1; i<LINES ; i++){ //check each line
getline(archivo,temporal); //pass the line to a temporal string
if(temporal[0]!='\"'){ //check if the line begins with "
cout<<"Error on line "<<i<<": \'\"\' was expected on first character"<<endl;
} else {
bool nextchar = true; //next character of the line
for(int j=1; j<MAXIMUMCHAR && nextchar == true; j++){
if(temporal[j]!= '\"' ){ //if line hasn't ended (there isn't a " )
(*(lines+i))[j] = temporal[j]; //write the char on the line string
} else {
cout<<"\nEnd of line "<<i<<" found\n";
nextchar = false; //loop exit
}
}
}
}
}
} else {
cout<<"File not found!"<<endl;
progfailed = true;
}
archivo.close();
if(progfailed==false){ //
cout<<"Result:"<<endl; //
for(int l=1; l<LINES ;l++){ //if everything was succesful
cout<<"Line "<<l<<": "<<lines[l]<<endl; //show the content of each line
}
}
cout<<endl;
cin.ignore();
cin.get();
return 0;
}
And here's the... uh... output... truncated of course, as it's a lot.
Code:
End of line 1 found
End of line 2 found
Error on line 3: '"' was expected on first character
Error on line 4: '"' was expected on first character
Error on line 5: '"' was expected on first character
Error on line 6: '"' was expected on first character
Error on line 7: '"' was expected on first character
Error on line 8: '"' was expected on first character
Error on line 9: '"' was expected on first character
Error on line 10: '"' was expected on first character
Error on line 11: '"' was expected on first character
Error on line 12: '"' was expected on first character
Error on line 13: '"' was expected on first character
Error on line 14: '"' was expected on first character
Error on line 15: '"' was expected on first character
Error on line 16: '"' was expected on first character
Error on line 17: '"' was expected on first character
Error on line 18: '"' was expected on first character
Error on line 19: '"' was expected on first character
Result:
Line 1: sfsdf es una lineaa\╪&= v98 NUMBER_OΦ&= OCESSORS=1 O°&= ndows_NT Pa'= \D
evc\Bin;C:↑'= c\lib\gcc-li('= ngw32\3.2;C:8'= c\Bin;C:\DevH'= b\gcc-lib\miX'= 2\
3.2;C:\Arch'= s de programx'= rland\Delphię'= n;C:\Archivo˙'= programa\Boż'= d\
Delphi7\Pr╕'= ts\Bpl\;C:\W╚'= WS\system32; INDOWS;C:\W(=
........"esta es u A╪'= ♂ ........"esta es un (= rchivos de programa\8(= ivos comu
What is wrong here? I'd be grateful if someone could please take a look at it.
Thanks in advance, and excuse my poor programming skills