Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:44:06+00:00 2026-06-15T05:44:06+00:00

My code opens a text file, counts the number of lines, allocates an array

  • 0

My code opens a text file, counts the number of lines, allocates an array to store all lines and then calls a function to fill this array with each line. This function file.getline calls return empty strings:

Here’s the code:

typedef char* line;

…

char* filename=new char[256];
cout << "Type a file name: " << endl;
cin.ignore();
cin.getline(filename,255);

ifstream iFile(filename);

int nLines=CountLines(iFile);

line* LineArray = new line[nLines];
ReadLines(LineArray,iFile);

CountLines function:

int CountLines(ifstream &file)
{
line templine=new char[64];
int nLines=0;

while (!file.eof())
{
    file.getline(templine,64);

    if (*templine != '\n')
        nLines++;

}
delete [] templine;

return nLines;
}

This works properly. ReadLines however does not:

void ReadLines(line* LineArray, ifstream &file)
{
    line templine=new char[64];

file.seekg(0,ios::beg);

int i = 0;
while (!file.eof())
{

    if (*templine != '\n')
    {
        LineArray[i]=templine;
        i++;
    }

}
delete [] templine;
}

I have a feeling that it has something to do with the ‘\n’ issue of getline but as I set the get pointer to 0 and the file starts with normal text and not a line, I can’t understand why it fills templine with empty strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:44:09+00:00Added an answer on June 15, 2026 at 5:44 am

    There are too many bugs in your code.

    • Parameters for istream::getline() is wrong
    • You need clear eof flag after CountLines()
    • Wrong memory free operation.
    • blah blah …

    Pointers are not toys, you’d better go with Tino Didriksen’s solution.

    If you really like char and pointers, it should look like this:

    #include <iostream>
    #include <fstream>
    #include <cassert>
    
    using namespace std;
    
    int CountLines(ifstream &fin) {
      char templine[1024];      // no need for dynamic allocation.
      int count = 0;
      while (fin.getline(templine, 1024))
        count++;
      return count;
    }
    
    void ReadLines(char** lines, int count, ifstream &fin) {
      fin.seekg(0, ios::beg);
      for (int i = 0; i < count; i++) {
        lines[i] = new char[1024];      // you need dynamic allocation here.
        fin.getline(lines[i], 1024);
        assert(fin.gcount() < 1024);    // assure the line is shorter than 1023 chars
      }
    }
    
    int main() {
    
      char filename[256];         // no need for dynamic allocation.
      cin.getline(filename, 256); // second parameter should be the same size of your buffer.
    
      ifstream fin(filename);
    
      int count = CountLines(fin);
      char** lines = new char*[count];
    
      // After CountLines() called, fin.eof is set, you need to clear it.
      // Otherwise fin.getline() won't do a thing.
      fin.clear();
      ReadLines(lines, count, fin);
    
      // When every thing is done, you need to free all the memory.
      for (int i = 0; i < count; i++)
        delete[] lines[i];
      delete[] lines;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to count the number of lines in a text file. This is
I wrote a code to count the number of lines in all the files
I am trying to open a text file in python. In the following code:
When I open a file using this code if (ofd.ShowDialog() == DialogResult.OK) text =
I have written code that opens 16 figures at once. Currently, they all open
I have some AS2 code that opens a SWF file through an HttpHandler. I'm
Is there any way to have a code that opens a PDF file in
To start out with, I basically have a function that opens up a text
I have a text file containing 5-line chunks of tab-delimited lines: 1 \t DESCRIPTION
I need to read a large space-seperated text file and count the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.