I have a general question.
I am trying to read a file named: 1.0.0.txt but this seems not to be possible.
The MessageBox shows emty.
If I trying to read a file ex named: File1.txt it works.

So I wonder why it is not possible to read this file named 1.0.0.txt and if it is possible in any way because I have created a hiarchy of files following numbers.
Thanks !

std::string Line;

ifstream GetLine1("C:\\1.0.0.txt"); 
while( getline(GetLine1,  Line) )       
{
}

String^ Line2 = gcnew String(Line.c_str());
MessageBox::Show(Line2);

Recommended Answers

All 2 Replies

I have a general question.
I am trying to read a file named: 1.0.0.txt but this seems not to be possible.
The MessageBox shows emty.
If I trying to read a file ex named: File1.txt it works.

So I wonder why it is not possible to read this file named 1.0.0.txt and if it is possible in any way because I have created a hiarchy of files following numbers.
Thanks !

std::string Line;

ifstream GetLine1("C:\\1.0.0.txt"); 
while( getline(GetLine1,  Line) )       
{
}

String^ Line2 = gcnew String(Line.c_str());
MessageBox::Show(Line2);

It has nothing to do with the filename. "C:\\1.0.0.txt" should work fine. It has to do with whether the last line in the file has a newline at the end of it. If it does, you're going to get an empty string stored in Line when you're done. If it doesn't, Line will contain the last line with characters in the file. You have an empty while statement. Do you want that? Try this program:

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    std::string Line;

    ifstream GetLine1 ("C:\\1.0.0.txt"); 
    while( getline(GetLine1,  Line) )       
    {
        cout << Line << endl;
    }
    
    cout << Line << endl;
    GetLine1.close ();
    return 0;
}

with this input file:

Harry
Bill
Simon
Jeff

Try it both with the cout line in the while statement as it is now and commented out. Also try it when there is a newline after the word Jeff in the file and when there isn't. If you comment out the cout statement inside the while loop and put a newline after "Jeff", you should get nothing displayed to the screen and Line will contain "". If there's no newline, Line will contain "Jeff".

Yes that was the problem. I had a newline character in the file. I took this away and this did work.
It is ok for me to have an emty whilestatement though I am just after the "Line" so I wont do anything more.
This saved the day with my hiarchysystem of files :)
Thanks...


It has nothing to do with the filename. "C:\\1.0.0.txt" should work fine. It has to do with whether the last line in the file has a newline at the end of it. If it does, you're going to get an empty string stored in Line when you're done. If it doesn't, Line will contain the last line with characters in the file. You have an empty while statement. Do you want that? Try this program:

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    std::string Line;

    ifstream GetLine1 ("C:\\1.0.0.txt"); 
    while( getline(GetLine1,  Line) )       
    {
        cout << Line << endl;
    }
    
    cout << Line << endl;
    GetLine1.close ();
    return 0;
}

with this input file:

Harry
Bill
Simon
Jeff

Try it both with the cout line in the while statement as it is now and commented out. Also try it when there is a newline after the word Jeff in the file and when there isn't. If you comment out the cout statement inside the while loop and put a newline after "Jeff", you should get nothing displayed to the screen and Line will contain "". If there's no newline, Line will contain "Jeff".

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.