Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Commented data file?

Commented data file?

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    knapak
    wrote on last edited by
    #1

    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!

    C 1 Reply Last reply
    0
    • K knapak

      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!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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++

      K 1 Reply Last reply
      0
      • C Christian Graus

        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++

        K Offline
        K Offline
        knapak
        wrote on last edited by
        #3

        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!

        C 1 Reply Last reply
        0
        • K knapak

          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!

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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++

          K 1 Reply Last reply
          0
          • C Christian Graus

            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++

            K Offline
            K Offline
            knapak
            wrote on last edited by
            #5

            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!

            C T 2 Replies Last reply
            0
            • K knapak

              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!

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              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++

              1 Reply Last reply
              0
              • K knapak

                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!

                T Offline
                T Offline
                Tom Archer
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups