The below code simply reads the string "This is a test" from a text file and displayes it on the console.

I've deliberately placed an endline at the end of each time the string is loaded to show that the string is all that is passed to 'mystring' from 'myfile'.

Is there a way in C++ to increase the number of characters picked up by 'myfile' ? Say, for the first loop of while instead of 'myfile' only picking up "This", it picks up "This i" and on the next loop it picks up the next 6 characters including white space "s a te" etc?

At the moment, 'myfile' only takes in everything up to the first white space.

Thanks

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

using namespace std;

int main()
{
    ifstream myfile;
    string mystring;

    myfile.open("myfile.txt");

    while (!myfile.eof())
    {
        myfile>>mystring;
        cout << mystring << endl;
    }

    myfile.close();
}

Recommended Answers

All 8 Replies

This way worked. However, many spaces are printed after reading the last 't'.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    ifstream myfile;
    char mystring;
    myfile.open("myfile.txt");
    myfile>>noskipws;
    while (!myfile.eof())
    {   
        for(int i=0;i<6;i++){
        myfile>>mystring;
        cout << mystring;
        }
        cout<< endl;
    }
    myfile.close();

    system("PAUSE");
    return 0;
}

Have you ever used getline()? you would have to use a char* but it should work nicely.

NathanOliver: There is actually a getline() for std::string as well. istream& getline (istream& is, string& str);

@Tumlee I know there is one for strings but it does not have a way to control how many characters are received in. The getline for char* does have that ability though.

Thanks, this actually works well. Agreed that you can't control the width using getline though I could still use this in some cases.

I opened this back up because there's on small related area I'm unsure about. Krishal, your code works perfectly but I noticed when parsing a pdf file and sending the output to a consol, some of the usual readable characters (when reading stream data into a string) aren't readable anymore. They're either encoded in some form (some looks cyrillic) or they're corrupted. I don't get the same problem when placing the input into a string in c++, but using characters I do.

I would assume this is due to the fact that characters aren't null terminated (possibly). If I write the character output to a text file they display fine. Clearly there's a fundamental floor in my understanding of characters and strings so would anyone be able to shead some light on why the above happens.

Thanks

Pdf files are compact(maybe that is why it would seem corrupted. harder to parse) whereas a text file is an electronic and computer structured(that is why it easier to read a txt file).

Well that is what I think is happening. Correct me if I'm wrong.

I hope someone would explain better :)

Hi, thanks for coming back. Well, you're partially right about pdfs. They do have encoded streams in them though the metadata is all in simple english (not encoded/compressed form).

If I use a string to input the file stream contents and then use the same string to output these contents to the console I don't have a problem. If I use characters in the same way and print them to the console one by one I get some corrupted characters. If, when using characters I print the output to a text file I don't get any corruption. Your technique works perfectly as long as I'm not printing from pdf to console. I don't really need to print to console, a text file is better for what I'm doing anyway. I'm just interested in understanding why I get that corruption when using characters as opposed to a string.

This is why I suggested that it may have something to do with the consoles interpretation of some characters (with no null termination) as opposed to a string which is NULL terminated.

commented: Agreed. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.