Commented data file?
-
Hi guys Once again, please excuse me if this is way too simple... oh those newbies... :doh: If I have to read a file with many lines of data, it would be useful for the user to be able to identify any particular entry that wants to be modified. Of course, the code that reads the data (or the manual) tells what it is, but for the user it would be easier if information could be added to the file that has the data with the program ignoring those bits of information. I'd appreciate any simple solution to this. Thanks a million!
-
Hi guys Once again, please excuse me if this is way too simple... oh those newbies... :doh: If I have to read a file with many lines of data, it would be useful for the user to be able to identify any particular entry that wants to be modified. Of course, the code that reads the data (or the manual) tells what it is, but for the user it would be easier if information could be added to the file that has the data with the program ignoring those bits of information. I'd appreciate any simple solution to this. Thanks a million!
Do what all languages do - define a character sequence to indicate a comment. For example, in C++, it's // or /* ... */. Use a sequence that makes sense to your users, if they use VB, for example, use '. Then the trick is that any string that is valid and starts with ' needs to start with a known sequence to escape that character ( such as '' ). The other way to do it is to use XML, and then HTML comments are valid. Christian Graus - Microsoft MVP - C++
-
Do what all languages do - define a character sequence to indicate a comment. For example, in C++, it's // or /* ... */. Use a sequence that makes sense to your users, if they use VB, for example, use '. Then the trick is that any string that is valid and starts with ' needs to start with a known sequence to escape that character ( such as '' ). The other way to do it is to use XML, and then HTML comments are valid. Christian Graus - Microsoft MVP - C++
uh... either I didn't make myself clear or I didn't understand your response. Of course I know that INSIDE THE CODE I can use // or /*...*/. The question is how do I comment in a file to be read by the program, making the program ignore the comment... just the same as in the code but while reading the file. Say I have the following code:
int main() { int id; double Stuff; ifstream GetData; GetData.open("D:\\SSL\\Code\\Testing_Rutines\\FileComms\\comms.dat"); if (! GetData.is_open()) { cout << "Error opening data file" << endl; exit (1); } else cout << "Commented file opened" << endl; while(!GetData.eof()) { GetData >> id >> Stuff; cout << id << Stuff; } GetData.close(); return 0; }
And the data file has (with comments): // first 1 2.3 // second 2 3.5 // third 3 9.8 where the program needs to ignore "// first" , "// second" and "// third". If your response is that I need to "define a character sequence to indicate a comment" I have absolutely no idea how to do it. Both // and /*...*/ are already defined in C++ as comments inside the code, I don't have to define anything. Thank you! -
uh... either I didn't make myself clear or I didn't understand your response. Of course I know that INSIDE THE CODE I can use // or /*...*/. The question is how do I comment in a file to be read by the program, making the program ignore the comment... just the same as in the code but while reading the file. Say I have the following code:
int main() { int id; double Stuff; ifstream GetData; GetData.open("D:\\SSL\\Code\\Testing_Rutines\\FileComms\\comms.dat"); if (! GetData.is_open()) { cout << "Error opening data file" << endl; exit (1); } else cout << "Commented file opened" << endl; while(!GetData.eof()) { GetData >> id >> Stuff; cout << id << Stuff; } GetData.close(); return 0; }
And the data file has (with comments): // first 1 2.3 // second 2 3.5 // third 3 9.8 where the program needs to ignore "// first" , "// second" and "// third". If your response is that I need to "define a character sequence to indicate a comment" I have absolutely no idea how to do it. Both // and /*...*/ are already defined in C++ as comments inside the code, I don't have to define anything. Thank you!I'm saying that you read the data a line at a time, and check each line for the comment delimiter, and if it is there, ignore it for processing. Christian Graus - Microsoft MVP - C++
-
I'm saying that you read the data a line at a time, and check each line for the comment delimiter, and if it is there, ignore it for processing. Christian Graus - Microsoft MVP - C++
That sounds as if you were recommending to read both the data and the comment and then just ignore/destroy the comment read. If so, that is something I don't want to do. I want to read the data only and if a comment is found ignore it, do not read it. Thanks!
-
That sounds as if you were recommending to read both the data and the comment and then just ignore/destroy the comment read. If so, that is something I don't want to do. I want to read the data only and if a comment is found ignore it, do not read it. Thanks!
Then you're screwed. You can't know that a comment exists without reading it from disc and examining it. Christian Graus - Microsoft MVP - C++
-
That sounds as if you were recommending to read both the data and the comment and then just ignore/destroy the comment read. If so, that is something I don't want to do. I want to read the data only and if a comment is found ignore it, do not read it. Thanks!
As Christian is saying, if you're going to use a file where you're manually reading every line, then by definition, you'll have to manually parse the comment lines and ignore them. This is one argument for using XML as with XML you can specifically read data-only nodes and not read comment-nodes.