again, im working on a parser, is there way to determine what line of code your on.
I was thinking of writing something that goes through the whole file, and checks for '\n's that would work right?
again, im working on a parser, is there way to determine what line of code your on.
I was thinking of writing something that goes through the whole file, and checks for '\n's that would work right?
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
>is there way to determine what line of code your on
std::cout<< __LINE__;
-Prelude
My best code is written with the delete key.
ok I solved that problem now, in a file, how can I go directly to a line?
Here is what I came up with for the number of lines
the out file is used laterCode:#include <iostream.h> #include <fstream.h> #include <stdlib.h> int main(int argc, char* argv[]) { if(argc>1) abort(); char a; int count=0,lines=0; ifstream in; ofstream out; in.open("c:\\luke\\new.txt"); out.open("c:\\luke\\new2.txt"); while(in.get(a)) { if(a=='\n') lines++; } cout<<lines<<endl; in.close(); out.close(); return 0; }
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
>how can I go directly to a line?
If you want to be gutsy you can format the file so that the offsets are properly sequenced and then you can seek to the line you want, something like this:
file.seekg ( line_num * LINE_BYTES );
Or you could keep it simple and just read your way to the line.
-Prelude
My best code is written with the delete key.
>You can only use seek() to get to a line if you've previously produced an index.
This is true for text files, but binary files are not restricted in this manner (I'm sorry for not being more clear on what I meant). According to the C++ standard, fstream::seekg calls filebuf::seekoff which in turn calls the equivalent to fseek. fseek is defined as follows for binary and text streams:
-PreludeCode:For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified by whence. The specified position is the beginning of the file if whence is SEEK_SET, the current value of the file position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END. For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file and whence shall be SEEK_SET.
My best code is written with the delete key.