468,510 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

How to determine number of lines in a text file to return an array

4
Hi,

I'm new to C and I'm trying to read in a file that can have an arbitrary number of lines, and put each line into a string array. I guess my problem is that I dont't want to have to explicitly create a new array with a certain size since the number of lines can always change (in the code below I use 101). Is there a better way to return an array or a pointer to an array that contrains all the lines. This is what I have so far.

Expand|Select|Wrap|Line Numbers
  1.  
  2. string* test()
  3. {    
  4.     string line;
  5.     string *vol= new string[101];
  6.     int i = 0;
  7.  
  8.     ifstream myfile("VOL.txt", ios::in);
  9.  
  10.     if (myfile.is_open())
  11.     {
  12.         while(!myfile.eof() && i != 101)
  13.         {
  14.             getline (myfile, line);
  15.  
  16.             if (line != firstLine)
  17.             {
  18.                 vol[i] = line;
  19.                 i++;
  20.             }
  21.         }
  22.         myfile.close();
  23.     }
  24.  
  25.     return vol;
  26. }
  27.  
Jul 5 '07 #1
4 2236
archonmagnus
113 100+
You will have to read the entire contents of the file first. You could insert a section of code like the following:

Expand|Select|Wrap|Line Numbers
  1. // ... previous code
  2.  
  3. if (myfile.good())
  4. {
  5.     do
  6.     {
  7.         myfile>>char;
  8.         if (char == '\n') lines++;
  9.     } while (!myfile.fail());
  10.  
  11.     myfile.clear()   // resets the EOF bit so the file can be read again
  12.     myfile.seekg(0);  // make sure the "get" pointer is at the beginning of the file
  13.  
  14.     string *vol= new string [lines];
  15.  
  16.     // ... trailing code follows
  17. }
  18.  
Jul 5 '07 #2
scruggsy
147 100+
Or, use a vector instead of an array, and just push each new line onto the vector as it's read, letting the vector take care of resizing itself if needed.
Jul 5 '07 #3
weaknessforcats
9,207 Expert Mod 8TB
You are using C++. Arrays are low-level containers that C++ has inherited from C and you are coming up against the first problem with arrays: they are fixed.

In C++ use a vector<string>.

As you add strings to the vector it expands. So you just read the the lines from your file and do a push_back() for each line. No need to read the file and count lines, etc.
Jul 6 '07 #4
CK938
4
Thanks for all your help. I think I got it.
Jul 6 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Vasilis Serghi | last post: by
6 posts views Thread by Tom McLaughlin | last post: by
14 posts views Thread by Paul_Madden via DotNetMonster.com | last post: by
6 posts views Thread by magix | last post: by
6 posts views Thread by shana07 | last post: by
158 posts views Thread by jty0734 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.