Reading txt file with ifstream goes wrong
Apr 9, 2012 at 1:53pm UTC
Hi,
I have the following code that reads an ip address ( i.e. 192.168.0.199) from a txt file. The txt file has only 1 line and that is the ip address.
The readfile (); read the ip address into ipaddr OK, but when returning to caller mess the ipaddr up.
Can anyone see what I am doing wrong here?
rgs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
int readfile()
{
ifstream stream1("D:\\file1.txt" );
char a[80];
if (!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
while (!stream1.eof())
{
stream1 >> a;
string ipaddr = a;
//Sleep ( 1000);
}
return (0);
}
Last edited on Apr 9, 2012 at 1:55pm UTC
Apr 9, 2012 at 2:05pm UTC
Your program is probably over-writing the value before it finishes. Try a vector approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <vector>
int readfile()
{
ifstream stream1("D:\\file1.txt" );
char a[80];
std::vector<std::string> readdata;
if (!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
while (!stream1.eof())
{
stream1 >> a;
readdata.push_back(a); //now readdata is an array with everything read from the file
//Sleep ( 1000);
}
return (0);
}
Apr 9, 2012 at 3:02pm UTC
Hi,
Thanks, now I am almost OK
I now have the ip address in the readdata [1], but how do I
write it to the ipaddr variable that is defined like char *ipaddr
See my code below.
I tried with ipaddr = readdata [1], but ...
rgs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include "settings.h"
#include<iostream>
#include<fstream>
#include<Windows.h>
#include <vector>
using namespace std;
int readfile ();
char *ipaddr;
string readdata;
namespace iocp {
SettingsData::SettingsData()
:
IOCP_Port(8092) {
readfile ();
hostAddress.s_addr = inet_addr(ipaddr);
}
SettingsData Settings;
} // end namespace iocp
int readfile()
{
ifstream stream1("D:\\file1.txt" );
char a[80];
std::vector<std::string> readdata;
if (!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
while (!stream1.eof())
{
stream1 >> a;
readdata.push_back(a); //now readdata is an array with everything read from the file //Sleep ( 1000);
}
return (0);
}
Last edited on Apr 9, 2012 at 3:04pm UTC
Apr 9, 2012 at 3:27pm UTC
Here's the question, why do you want to use char*? if you just want to pass it to a function, you can use the method of std::string::c_str(), which returns a const char* to your string, you can use it as a C-String to pass it to some function as well:
std::cout << readdata[1].c_str() << std::endl; //you're printing a c-string here
Now if you want to copy it to a char* for some other purpose, use the strcpy:
1 2 3 4
#include <string.h>
char * mystring = new char [readdata[1].size() + 1]; // + 1 because you're converting to a null terminated character
strcpy(mystring, readdata[1].c_str()); //this copies your string to mystring
I didn't test this code, it's just from the top of my head, so good luck :-)
Last edited on Apr 9, 2012 at 3:30pm UTC
Apr 9, 2012 at 3:46pm UTC
Don't forget to replace
1 2 3 4 5
while (!stream1.eof())
{
stream1 >> a;
readdata.push_back(a);
}
with
1 2 3 4
while (stream1 >> a)
{
readdata.push_back(a);
}
Because you used "while(!stream1.eof())", your loop actually attempts to read past the end of file and adds one extraneous element to readdata, an empty string.
Topic archived. No new replies allowed.