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. Problem in File seek()

Problem in File seek()

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 6 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.
  • U Offline
    U Offline
    User 4396598
    wrote on last edited by
    #1

    Hi All, I am reading some big files having thousands of lines, my requirement is to write some thing on the last but one line(i know the length of last line). how to seek my file pointer to last but one line with out seeking all the position from the beginning of the file? i tried with some code, but not achieving the right goal. please check the code

    		long seekval = assfile.SeekToEnd();
    		assfile.Seek((seekval - 4.0),0);
    		assfile.WriteString(\_T("some text"));
    

    assfile is the CStdioFile obj. 4 is the length of the last line. Thanks in advance. Regards, Kumar

    L Mircea PuiuM L U 4 Replies Last reply
    0
    • U User 4396598

      Hi All, I am reading some big files having thousands of lines, my requirement is to write some thing on the last but one line(i know the length of last line). how to seek my file pointer to last but one line with out seeking all the position from the beginning of the file? i tried with some code, but not achieving the right goal. please check the code

      		long seekval = assfile.SeekToEnd();
      		assfile.Seek((seekval - 4.0),0);
      		assfile.WriteString(\_T("some text"));
      

      assfile is the CStdioFile obj. 4 is the length of the last line. Thanks in advance. Regards, Kumar

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A quick look at the MSDN[^] page suggests this answer:

      assFile.Seek(-4, CFile::end);
      

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      1 Reply Last reply
      0
      • U User 4396598

        Hi All, I am reading some big files having thousands of lines, my requirement is to write some thing on the last but one line(i know the length of last line). how to seek my file pointer to last but one line with out seeking all the position from the beginning of the file? i tried with some code, but not achieving the right goal. please check the code

        		long seekval = assfile.SeekToEnd();
        		assfile.Seek((seekval - 4.0),0);
        		assfile.WriteString(\_T("some text"));
        

        assfile is the CStdioFile obj. 4 is the length of the last line. Thanks in advance. Regards, Kumar

        Mircea PuiuM Offline
        Mircea PuiuM Offline
        Mircea Puiu
        wrote on last edited by
        #3

        Read the content of the file, modify what you need, write the file back. You cannot modify a line directly in a text file.

        SkyWalker

        N 1 Reply Last reply
        0
        • U User 4396598

          Hi All, I am reading some big files having thousands of lines, my requirement is to write some thing on the last but one line(i know the length of last line). how to seek my file pointer to last but one line with out seeking all the position from the beginning of the file? i tried with some code, but not achieving the right goal. please check the code

          		long seekval = assfile.SeekToEnd();
          		assfile.Seek((seekval - 4.0),0);
          		assfile.WriteString(\_T("some text"));
          

          assfile is the CStdioFile obj. 4 is the length of the last line. Thanks in advance. Regards, Kumar

          L Offline
          L Offline
          loyal ginger
          wrote on last edited by
          #4

          If you are using CFile, then the second parameter for the Seek() method is from where to seek. In your case you should seek from the end of the file, using CFile::end. In your code, you are seeking from the beginning of the file. --modification-- Sorry, Richard MacCutchan had already answered the question. Ignore mine, please!

          1 Reply Last reply
          0
          • Mircea PuiuM Mircea Puiu

            Read the content of the file, modify what you need, write the file back. You cannot modify a line directly in a text file.

            SkyWalker

            N Offline
            N Offline
            normanS
            wrote on last edited by
            #5

            Are you sure about that? My memory - not that I've done file handling in the last 4 or 5 years - says that you can modify whatever you want, you just can't add any characters or take away any characters (so you can't add a line of text to a text file, but you can change "Paul" to "Fred".

            1 Reply Last reply
            0
            • U User 4396598

              Hi All, I am reading some big files having thousands of lines, my requirement is to write some thing on the last but one line(i know the length of last line). how to seek my file pointer to last but one line with out seeking all the position from the beginning of the file? i tried with some code, but not achieving the right goal. please check the code

              		long seekval = assfile.SeekToEnd();
              		assfile.Seek((seekval - 4.0),0);
              		assfile.WriteString(\_T("some text"));
              

              assfile is the CStdioFile obj. 4 is the length of the last line. Thanks in advance. Regards, Kumar

              U Offline
              U Offline
              User 4674874
              wrote on last edited by
              #6

              First Read the last line Move this line by the length of line you want to add in front. Then paste the required line above it I hope this will do

              ashwani

              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