472,958 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 2475
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?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.