2

I want to be able to read a full line into a character array using a function.

example input text is:

Schmidt, Helga
Alvarez, Ruben
Zowkowski, Aaron
Huang, Sun Lee
Einstein, Beverly

However, im not sure how to read a full line of characters into the array. I know the delimiter for >> is whitespace, but I'm not sure if I change that delimiter to '\n' if it'd work?

void buildList(char (*array)[25], ifstream& inputFile){
    string line;
    for (int i = 0; i < 5; i++)
        getline(inputFile, line);
        array[i] = line.c_str();     
}

Currently this only reads either a last name or first name into my input instead of the whole line. I'm not sure how I can go about changing this. Thanks.

7
  • @JoeZ that returns a string, not char* though. Commented Nov 18, 2013 at 16:17
  • Try using getline. Here is how. You can always convert string to char* array. Commented Nov 18, 2013 at 16:17
  • @WorldDominator It takes a string as an output parameter, to be precise. And of course you can always get the cstr of any string. Commented Nov 18, 2013 at 16:20
  • I really would recommend you use the std::string version of getline, but if you absolutely insist on not, there are versions that are members of the streams that work with a raw char buffer here. Commented Nov 18, 2013 at 16:25
  • @Hulk I just edited my code to what I believe you recommended but the compiler gives me the issue of "incompatible types in assignment of 'const char*' to 'char [25]'". I'm not sure if c_str helps me. Commented Nov 18, 2013 at 16:28

2 Answers 2

5

First, you definitely want to use std::string here. Once you do that, you can use std::getline:

std::vector<std::string>
buildList( istream& input )
{
    std::vector<std::string> results;
    std::string line
    while ( std::getline( input, line ) ) {
        results.push_back( line );
    }
}

This will make for much simpler and more robust code.

If you have to use such a broken interface, there is a member function getline:

for ( int i = 0; i != 5; ++ i ) {
    input.getline( array[i], maxLength );
}

Also: a function should never take an std::ifstream& as argument (unless it is going to open or close the file). An std::istream& should be used.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you James! the input.getline worked splendid. My professor said we should use char ** for this assignment so that's why I did it this way. Also, he has not gone over vectors yet. I'll keep the istream& in mind.
Out of curiosity, where is the memory allocated for array[]? ie. what set up all the char * in the array?
@WorldDominator I susspected something along these lines. Logically, I can't believe that someone would introduce you to declarations along the lines of char (*array)[25] (a pointer to an array of 25 char, or a pointer to the first of many such) before teaching you std::vector and std::string, but I keep seeing it.
@JoeZ At the call site. My guess is that he has something like char data[5][25];, and passes data as an argument. What gets me, however: doing this right (and if that's his data declaration, he's done it right) requires knowing some of the more arcane aspects of the C++ type system. It would be one of the last things I'd teach.
@JamesKanze To be honest, the teacher is very poor at teaching and hasnt been in class for 2 weeks now. I've learned as much as I would have if he had been here though. He said we had to initialize the array to array[5][25] in our main function (I'm using header files). So that's why I passed char (*array) [25].
|
1

Use this-

void buildList(char (*array)[25], ifstream& inputFile){
    for (int i = 0; i < 5; i++)
        std::inputFile.getline(array[i],50);
}

The second parameter of getline is the maximum number of characters to write to the character array.

7 Comments

if I use getline I get the error: no matching function for call to 'getline(std::ifstream&, char [25])'
The function is declared in std namespace.
I'm using namespace std though. Isn't it because instead of a string, I used a char[25]?
Oh yes, I overlooked that, I'll just edit the answer.
This is completely wrong. The getline that takes a char* is a member of std::istream, and must use the member syntax in the call. It also requires a maximum length as a second argument.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.