OK im ok with some baisc codeing but i cose to go a little farther and write some more in detailed stuff so i started making a few fuctions. iv done this once before, but i cant remeber what i did diffrent.

The function takes a file and reads each line untill its read X number of lines, then returns that line.
what am i doing wrong!!!

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//
// The following function reads a line specified by the function, and outputs it.

string read_file(string file, int line)
{
       string current_line;
       
       int i;
       
       ifstream current_file(file.c_str());
       
       
       
       
       for( i = 1; i < line + 1; i++)
       {
            
              getline (current_file, current_line);
       
       }
       
       cout << current_line;
       current_file.close();
       
       return current_line;

       
       
}

And what exactly is the problem?

You have no syntax errors, and I can't see anything wrong. But i'm sleepy, so maybe I'm wrong...
Please be more specific

The program won't work if the file has fewer than line number of lines. Here's one way to correct that

int i = 0;
while(i < line && getline (current_file, current_line))
    ++i;
if(i != line) // if error
    return ""; // return blank line
return line;

The program won't work if the file has fewer than line number of lines. Here's one way to correct that

int i = 0;
while(i < line && getline (current_file, current_line))
    ++i;
if(i != line) // if error
    return ""; // return blank line
return line;

YEah the issue is that it always returned nothing. i couldnt figure out why, the getline code should be working, lemme try this and see if i can get it working. thanks

This is the full program as i currently have it. its not meant to do anythign then read a single file called test.txt and output the first line.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//
// The following function reads a line specified by the function, and outputs it.

string read_file(string file, int line)
{
       string current_line;
       
       
       ifstream current_file(file.c_str());
       
       
       
       
       int i = 0;
while(i < line && getline (current_file, current_line))
    ++i;
if(i != line) // if error
    return ""; // return blank line
return current_line;

       
       
}


int main()
{
    string display;
    cout << read_file("test.txt", 1);
    
    
    
    cin.get();

    
}

test.txt is as follows

1
2
3
4
5
6
7
8
9

OOps! I think this is wrong

if(i != line) // if error
    return ""; // return blank line
return current_line;

should be this

if(i == line) // if error
    return ""; // return blank line
return current_line;

from what i see the function still returns nothing. i didnt think id have this much problem.

from what i coded im looking to get the value of the first line, followed by a cin.get() wait for a key entree. but im only getting the key press requirement.

i dont know what im missing, i just dont get any text at all when i thought i should be.

ifstream current_file;
current_file.open(file.c_str());

// do stuff

current_file.close();

Does that work?

I tired to use the .open() instead of the implied open, still no text. im confused... ill have to look it up later

Why don't you check if it's open?

current_file.open(file.c_str());
if (!current_file){
    cout<<"File not opened!\n"
    return "";
}
This article has been dead for over six months. Start a new discussion instead.