Aug 29, 2009 at 2:00pm UTC
i had a question regarding fstream
suppose i had a text file
1 850 9395 11.40am 6
2 900 10000 12.30pm 7
3 900 16800 10.30am 15
.....................
is there any command line that enable us to read a specific row into the program??
Aug 29, 2009 at 3:13pm UTC
I wrote my own library on this simple problem... use these:
1 2 3 4 5 6 7 8 9 10 11
string getLineStr(const char * file, int line) {
ifstream line_str(file);
int lineCounter = 0;
string lineStr;
while (lineCounter < line) {
getline(line_str, lineStr);
++lineCounter;
}
line_str.close();
return lineStr;
}
1 2 3 4 5 6 7 8 9 10 11 12 13
int getLineInt(const char * file, int line) {
ifstream line_int(file);
int lineCounter = 0;
string lineInt;
stringstream to_int;
while (lineCounter < line) {
getline(line_int, lineInt);
++lineCounter;
}
to_int << lineInt;
line_int.close();
return atoi(lineInt.c_str());
}
1 2 3 4 5 6 7 8 9 10 11
double getLineDbl(const char * file, int line) {
ifstream line_dbl(file);
int lineCounter = 0;
string lineDbl;
while (lineCounter < line) {
getline(line_dbl, lineDbl);
++lineCounter;
}
line_dbl.close();
return atof(lineDbl.c_str());
}
they are simple implementations but they get the job done. :)
PS... to set a specific line use these:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
void setLine(const char * file, string val, int line) {
ifstream line_file(file);
string fileStr;
string lineStr;
int counter = 1;
while (!line_file.eof()) {
if (counter != line) {
getline(line_file, lineStr);
lineStr += "\n" ;
if (counter == line+1) {
lineStr.clear();
}
} else {
lineStr = val + "\n" ;
}
fileStr += lineStr;
lineStr.clear();
++counter;
}
fileClear(file);
lineStr.erase(lineStr.length());
setStr(file, fileStr);
line_file.close();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
void setLine(const char * file, int val, int line) {
ifstream line_file(file);
string fileStr;
string lineStr;
int counter = 1;
while (!line_file.eof()) {
if (counter != line) {
getline(line_file, lineStr);
lineStr += "\n" ;
if (counter == line+1) {
lineStr.clear();
}
} else {
lineStr = itoa(val) + "\n" ;
}
fileStr += lineStr;
lineStr.clear();
++counter;
}
fileClear(file);
lineStr.erase(lineStr.length());
setStr(file, fileStr);
line_file.close();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
void setLine(const char * file, double val, int line) {
ifstream line_file(file);
string fileStr;
string lineStr;
int counter = 1;
while (!line_file.eof()) {
if (counter != line) {
getline(line_file, lineStr);
lineStr += "\n" ;
if (counter == line+1) {
lineStr.clear();
}
} else {
lineStr = dtostr(val) + "\n" ;
}
fileStr += lineStr;
lineStr.clear();
++counter;
}
fileClear(file);
lineStr.erase(lineStr.length());
setStr(file, fileStr);
line_file.close();
}
WARNING@@@!!!!! If more than one source is setting a file using
setLine()
there is a danger of the file being corupted!!!! (Never got around to making a completely safe one...)
Last edited on Aug 29, 2009 at 3:19pm UTC
Aug 30, 2009 at 1:26am UTC
....can i know what is the use of 'val' in the last three source code??
Aug 30, 2009 at 2:08pm UTC
in the last 3 val is what row line
is set to
setLine("text.txt" , "12:00" , 2);
sets row 2 to "12:00".
Last edited on Aug 30, 2009 at 2:10pm UTC
Aug 31, 2009 at 8:20am UTC
oo~~~~ i see~~~~~~~
thanks Alan~~~~~
i'll give it a try at this~~~~~~~
Aug 31, 2009 at 2:33pm UTC
thanks again Alan~~~~~
now i know how to continue my assignment again :D