0

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;

       
       
}
4
Contributors
9
Replies
10
Views
9 Years
Discussion Span
Last Post by Sci@phy
0

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

0

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;
0

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

0

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
0

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;
0

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.

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

// do stuff

current_file.close();

Does that work?

0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.