Posted By: PadMalcom
Read a text file line by line - 09/29/10 15:24
Hi, I have a text file and want to read one line after another into a String. The lines will contain characters like "\n" etc. Any ideas?
STRING* file_read_line(var myHandle, int nLineNr) { if (!myHandle) return ""; var oldPosition = file_seek(myHandle,0,4); STRING* res = str_create("#1000"); // Find the right line file_seek(myHandle,0,0); int i; int j; for (i=0;i<=nLineNr;i++) { str_cpy(res,""); j = file_str_readto(myHandle,res, "\r\n", 1000); if (j == -1) { file_seek(myHandle,oldPosition,0); return ""; } else { if (i == nLineNr) { file_seek(myHandle,oldPosition,0); return res; } } } }
int file_count_lines(var myHandle) { if (!myHandle) return -1; var oldPosition = file_seek(myHandle,0,4); file_seek(myHandle,0,0); int res = 0; int j = 0; STRING* tempStr = str_create("#1000"); while (1) { j = file_str_readto(myHandle,tempStr,"\r\n", 1000); if (j == -1) { break; } else { res +=1; } } file_seek(myHandle,oldPosition,0); return res; }