473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 2505
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,208 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

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

Similar topics

3
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files...
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
6
by: Tom McLaughlin | last post by:
How can I determine the numbers of lines of text that are visible in a textbox? Tom
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
6
by: shana07 | last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc. 3 12 4 64 7 8 10 128 ... I just wanna...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
158
by: jty0734 | last post by:
i don't know what input size of string is. so i can't gets inputsize before malloc function. i want determine the size of malloc without get inputsize in advance. how to deal with it?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.