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. Error in reading file

Error in reading file

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 6 Posters 2 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.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

    Anu

    P C K S D 5 Replies Last reply
    0
    • A Anu_Bala

      Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

      Anu

      P Offline
      P Offline
      Parthi_Appu
      wrote on last edited by
      #2

      It behaves correctly but your approach is wrong.. if you want to read different tag lenghts you can do it in two ways... 1) read byte by byte untill you fully read the tag name.... 2)just read first 16 bytes as you did and find out the excat length of tagname from the "tTagName" and then move the file pointer to the length+1 position (in your case DFC101 has length 6, so move the file pointer to position 7 from the begining of file) Hope this'll help

      Do your Duty and Don't expect the Result

      A 1 Reply Last reply
      0
      • A Anu_Bala

        Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

        Anu

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Anu_Bala wrote:

        I think it continously read 16 characters

        Of course it is: you asked to read 16 characters, so it is logical that the function will read 16 characters. I suggest that you store the complete line in a big buffer and 'parse' it after that. Copy char by char in the tTagName until you reach a blank space. Then start copying in the tDesc string. That's quite easy to do.


        Cédric Moonen Software developer
        Charting control

        A 1 Reply Last reply
        0
        • A Anu_Bala

          Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

          Anu

          K Offline
          K Offline
          kakan
          wrote on last edited by
          #4

          In addition to the answer from Cedric Moonen, it seems as if you can ignore lines starting with "//" since they doesn't contain useful information.

          1 Reply Last reply
          0
          • P Parthi_Appu

            It behaves correctly but your approach is wrong.. if you want to read different tag lenghts you can do it in two ways... 1) read byte by byte untill you fully read the tag name.... 2)just read first 16 bytes as you did and find out the excat length of tagname from the "tTagName" and then move the file pointer to the length+1 position (in your case DFC101 has length 6, so move the file pointer to position 7 from the begining of file) Hope this'll help

            Do your Duty and Don't expect the Result

            A Offline
            A Offline
            Anu_Bala
            wrote on last edited by
            #5

            How can i do like this? I dont know pls give me any sample..? For also copy char by char..

            Anu

            P 1 Reply Last reply
            0
            • C Cedric Moonen

              Anu_Bala wrote:

              I think it continously read 16 characters

              Of course it is: you asked to read 16 characters, so it is logical that the function will read 16 characters. I suggest that you store the complete line in a big buffer and 'parse' it after that. Copy char by char in the tTagName until you reach a blank space. Then start copying in the tDesc string. That's quite easy to do.


              Cédric Moonen Software developer
              Charting control

              A Offline
              A Offline
              Anu_Bala
              wrote on last edited by
              #6

              How can i read char by char..please give me any sample..

              Anu

              1 Reply Last reply
              0
              • A Anu_Bala

                How can i do like this? I dont know pls give me any sample..? For also copy char by char..

                Anu

                P Offline
                P Offline
                Parthi_Appu
                wrote on last edited by
                #7

                Hope you are using CFile.. if so use Seek method like, After find the length of tagname anf before reading the next time write this code, pFile.Seek(length+1, CFile::begin); char by char reading, is reading a single char use 1 as count in Read method pFile.Read(buff, 1); -- modified at 8:26 Wednesday 9th August, 2006

                Do your Duty and Don't expect the Result

                1 Reply Last reply
                0
                • A Anu_Bala

                  Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

                  Anu

                  S Offline
                  S Offline
                  Steen Krogsgaard
                  wrote on last edited by
                  #8

                  Take a look at scanf.

                  Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

                  1 Reply Last reply
                  0
                  • A Anu_Bala

                    Hi, Im reading data from file like this.. char tTagName[16] = {0} memset(tTagName,0,16); PFile.Read(tTagName,16);//tTagName is PRO01_BLC101 PFile.Read(tDesc,24);//tDesc is Liquid Level and the file looks like this... // PRO01_BLC101 Liquid Level % 2 // So it read the file fine and got correct output. But if the TagName is about 8 or 6 character it read the file wrongly..The file shows like this.. // DFC101 DEPROP FEED M3/HR 1 // I think it continously read 16 characters,so the tagname shows like "DFC101 DEPROP F " and tDesc is only "EED".. How can i avoid this..B'coz i have to work with different tagname length..It may be 4 or 8 or 16 or 20.. What can i do for this?

                    Anu

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Anu_Bala wrote:

                    char tTagName[16] = {0} memset(tTagName,0,16);

                    What are you trying to accomplish here? :confused: One or the other is sufficient, but not both. Use this as a starting point:

                    void main( void )
                    {
                    ifstream ifs("c:\\a.txt");
                    string sLine,
                    sTagName,
                    sDesc;

                    while (getline(ifs, sLine))
                    {
                        istringstream iss(sLine);        
                         
                        iss >> sTagName >> sDesc; // assumes a space delimeter
                    
                        cout << sTagName << "|" << sDesc << "|" << endl;
                    }
                    

                    }


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    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