3

I have learned my lesson, so i will be short, and to the subiect.

I need a function, in my class, that can read a file line by line, and store them into a array/string so i can use it.

I have the following example( please don`t laugh, i am a begginer):

int CMYCLASS::LoadLines(std::string Filename)
{

    std::ifstream input(Filename, std::ios::binary | ios::in);
    input.seekg(0, ios::end);
    char* title[1024];
    input.read((char*)title, sizeof(int)); 

    // here what ?? -_-

    input.close();


    for (int i = 0; i < sizeof(title); i++)
    {
            printf(" %.2X ";, title[i]); 
    }

    printf("\");

    return 0;
}
7
  • 1
    Use std::string and std:.vector. Use std::getline for reading. Or you might use an istream iterator, and at one time recommending it was "the" way to show that one belonged to the group of C++ insiders, but really it's just obfuscation. Explicit loops are good for communicating a looping action. Commented May 23, 2015 at 6:22
  • @Cheers and hth. - Alf thanks, but i cannot use while() to use getline... i need another solution Commented May 23, 2015 at 6:24
  • @Patrael: If "cannot usewhile" is a requirement from your teacher, then you'd better state the set of requirements in full, and verbatim. Other than that, it sounds meaningless. Commented May 23, 2015 at 6:25
  • I want to use this function in a console, so i can debug to see if it`s reading my lines properly, and beleive me if i use WHILE() the console stops, and my other stuff are not loading... using a continue; did not solve it... Commented May 23, 2015 at 6:28
  • stackoverflow.com/a/12506764/12711 Commented May 23, 2015 at 6:39

1 Answer 1

4

I'm not sure exactly what your are asking.

However - below is some code that reads a file line-by-line and stores the lines in a vector. The code also prints the lines - both as text lines and the integer value of each character. Hope it helps.

int main()
{
    std::string Filename = "somefile.bin";
    std::ifstream input(Filename, std::ios::binary | ios::in);  // Open the file
    std::string line;                                           // Temp variable
    std::vector<std::string> lines;                             // Vector for holding all lines in the file
    while (std::getline(input, line))                           // Read lines as long as the file is
    {
       lines.push_back(line);                                   // Save the line in the vector
    }

    // Now the vector holds all lines from the file
    // and you can do what ever you want it

    // For instance we can print the lines
    // Both as a line and as the hexadecimal value of every character

    for(auto s : lines)                                         // For each line in vector
    {
        cout << s;                                              // Print it
        for(auto c : s)                                         // For each character in the line
        {
            cout << hex                                         // switch to hexadecimal
                 << std::setw(2)                                // print it in two 
                 << std::setfill('0')                           // leading zero
                 << (unsigned int)c                             // cast to get the integer value
                 << dec                                         // back to decimal
                 << " ";                                        // and a space
        }
        cout << endl;                                           // new line
    }
    return 0;
}

I do not laugh due to your original code - no way - I was also a beginner once. But your code is c-style code and contains a lot of bugs. So my advice is: Please use c++ style instead. For instance: never use the C-style string (i.e. char array). It is so error prone...

As you are a beginner (your own words :) let me explain a few things about your code:

char* title[1024];

This is not a string. It is 1024 pointers to characters which can also by 1024 pointers to c-style strings. However - you have not reserved any memory for holding the strings.

The correct way would be:

char title[1024][256];  // 1024 lines with a maximum of 256 chars per line

Here you must make sure that the input file has less than 1024 lines and that each line each less than 256 chars.

Code like that is very bad. What to do if the input file has 1025 lines?

This is where c++ helps you. Using std::string you don't need to worry about the length of the string. The std::string container will just adjust to the size you put into in to it.

The std::vector is like an array. But without a fixed size. So you can just keep adding to it and it will automatically adjust the size.

So c++ offers std::string and std::vector to help you to handle the dynamic size of the input file. Use it...

Good luck.

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

1 Comment

thank you very much, this solved my problem! I cannot vote up cause i don`t have enough reputation yet :( But many thanks!

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.