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


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
  2. string* test()
  3. {    
  4.     string line;
  5.     string *vol= new string[101];
  6.     int i = 0;
  8.     ifstream myfile("VOL.txt", ios::in);
  10.     if (myfile.is_open())
  11.     {
  12.         while(!myfile.eof() && i != 101)
  13.         {
  14.             getline (myfile, line);
  16.             if (line != firstLine)
  17.             {
  18.                 vol[i] = line;
  19.                 i++;
  20.             }
  21.         }
  22.         myfile.close();
  23.     }
  25.     return vol;
  26. }
Jul 5 '07 #1
4 2475
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
  3. if (myfile.good())
  4. {
  5.     do
  6.     {
  7.         myfile>>char;
  8.         if (char == '\n') lines++;
  9.     } while (!myfile.fail());
  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
  14.     string *vol= new string [lines];
  16.     // ... trailing code follows
  17. }
Jul 5 '07 #2
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
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
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

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...
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...
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.
by: Tom McLaughlin | last post by:
How can I determine the numbers of lines of text that are visible in a textbox? Tom
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...
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...
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...
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...
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?
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...
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...
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...
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...
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...
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...
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...
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...
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.