How to read data from a .txt file in c++?

869 views
Skip to first unread message

omnetuser99

unread,
Nov 12, 2012, 6:19:27 AM11/12/12
to omn...@googlegroups.com
Hi,

In the inet framework, I need to read some data from a .txt file within inet framework. I have tried using normal c++ codes but they aren't working in omnet. Can someone provide me with a code which reads comma delimited/spearated data till the last line of .txt file.

Thank you.

Rudolf Hornig

unread,
Nov 12, 2012, 8:13:38 AM11/12/12
to omn...@googlegroups.com
OMNeT++ is a C++ library and your final model is a C++ program. You can do anything that you would do in normal C++

omnetuser99

unread,
Nov 12, 2012, 8:19:47 AM11/12/12
to omn...@googlegroups.com
Why does it then say that getline (http://msdn.microsoft.com/en-us/library/3ah895zy(v=vs.80).aspx) could not be resolved when the header files are included? This is the same with many other file handling functions, they are not being resolved although I have included the appropriate header files?

Rudolf Hornig

unread,
Nov 12, 2012, 8:22:10 AM11/12/12
to omn...@googlegroups.com
On Monday, November 12, 2012 2:19:47 PM UTC+1, omnetuser99 wrote:
Why does it then say that getline (http://msdn.microsoft.com/en-us/library/3ah895zy(v=vs.80).aspx) could not be resolved when the header files are included? This is the same with many other file handling functions, they are not being resolved although I have included the appropriate header files?

omnetuser99

unread,
Nov 12, 2012, 8:32:49 AM11/12/12
to omn...@googlegroups.com
Could someone please provide some basic code to take input from a file in c++ which is working on your omnet?

Jonas Eymann

unread,
Nov 12, 2012, 9:33:26 AM11/12/12
to omn...@googlegroups.com
there might be more elegant solutions, but those lines also work for
simple cases:

int MyModule::readInputFile(const char *filename)
{
std::ifstream filestream;
std::string line;

filestream.open(filename, std::ifstream::in);

if(!filestream) {
throw cRuntimeError("Error opening file '%s'?", filename);
return -1;
} else {
while ( getline(filestream, line) ) {
if (line.find('#') == 0) {
continue; // ignore comment lines
}
else {
parseInputLine(line);
}
}
}

return 0;
}

Am 12.11.2012 14:32, schrieb omnetuser99:
> Could someone please provide some basic code to take input from a file
> in c++ which is working on your omnet?
>
> On Monday, November 12, 2012 2:22:10 PM UTC+1, Rudolf Hornig wrote:
>
> On Monday, November 12, 2012 2:19:47 PM UTC+1, omnetuser99 wrote:
>
> Why does it then say that getline
> (http://msdn.microsoft.com/en-us/library/3ah895zy(v=vs.80).aspx
> <http://msdn.microsoft.com/en-us/library/3ah895zy(v=vs.80).aspx>) could
> not be resolved when the header files are included? This is the
> same with many other file handling functions, they are not being
> resolved although I have included the appropriate header files?
>
>
> https://plus.google.com/117818518802135929150/posts/5nJoGBHNz2Y
> <https://plus.google.com/117818518802135929150/posts/5nJoGBHNz2Y>
>
>
> On Monday, November 12, 2012 2:13:38 PM UTC+1, Rudolf Hornig wrote:
>
> OMNeT++ is a C++ library and your final model is a C++
> program. You can do anything that you would do in normal C++
>
> On Monday, November 12, 2012 12:19:27 PM UTC+1, omnetuser99
> wrote:
>
> Hi,
>
> In the inet framework, I need to read some data from a
> .txt file within inet framework. I have tried using
> normal c++ codes but they aren't working in omnet. Can
> someone provide me with a code which reads comma
> delimited/spearated data till the last line of .txt file.
>
> Thank you.
>
> --
> --
> Sent from the OMNeT++ mailing list. To configure your membership,
> visit http://groups.google.com/group/omnetpp
>
>
>


--
Jonas Eymann
Research Assistant
Institute of Communication Networks (E-4) Tel.: +49 40 42878-3444
Hamburg University of Technology Fax: +49 40 42878-2941
Schwarzenbergstr. 95 E Email: jonas....@tuhh.de
21073 Hamburg, Germany www.tu-harburg.de/et6
Reply all
Reply to author
Forward
0 new messages