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. C++ file I/O

C++ file I/O

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
6 Posts 5 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.
  • H Offline
    H Offline
    Hachaso
    wrote on last edited by
    #1

    Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!

    T X B D 4 Replies Last reply
    0
    • H Hachaso

      Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      strlen() function gives the length of a C string, when Sizeof returns the size in bytes of the variable passed as its parameter... when you write

      char *buf;
      sizeof(buf);

      it will always return 4 because on 32-bits architecture, pointers are 32 bits long, ie. 4 bytes...


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      1 Reply Last reply
      0
      • H Hachaso

        Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!

        X Offline
        X Offline
        xiaohe521
        wrote on last edited by
        #3

        char buf[100]; ifstream file("test.txt"); ... file.getline(buf, 100); you should do this I love Programming

        T 1 Reply Last reply
        0
        • X xiaohe521

          char buf[100]; ifstream file("test.txt"); ... file.getline(buf, 100); you should do this I love Programming

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          did you bother read his post ??:~ :suss:


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          1 Reply Last reply
          0
          • H Hachaso

            Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!

            B Offline
            B Offline
            Bob Stanneveld
            wrote on last edited by
            #5

            Hello, You can retrieve the extraction count. This is the amount of characters read from the last call to an unformatted input function. Look here[^] and here[^] for more information on gcount() and basic_istream respectively.

            int nCount = file.gcount();

            Hope this helps. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

            1 Reply Last reply
            0
            • H Hachaso

              Hi! I'm trying to write a small program that analyzes the text in a normal text file. I'm using the C++ ifstream methods. My question is: char *buf; buf = new char; ifstream file("test.txt"); ... file.getline(buf, 100); When I do this buf will contain a row of text. How do I do to know the size of buf?? I've tried sizeof(buf) but I get = 4 Debugging the code I see that buf has more then 4 character...counting them I get to 77. Any good tips! Thanks!

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

              Hachaso wrote: file.getline(buf, 100); If you are going to be asking for 100 bytes from the file, the buffer needs to be large enough. As it stands you are only allocating room for 1. Change the allocation to:

              buf = new char[100];

              Don't forget to delete it when done!


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

              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