Hi!
I'm having trouble reading text lines from a file describing the levels
for a simple game I'm creating.
Inside the function that reads the content of the file there is a loop
that basically looks like this
ifstream file("levels\\level1.txt");
string line;
getline(file, line);
while(!file.eof()){
...
getline(file, line);
}
The first part of the level file looks like this
#configs
levels/objectconfig.txt
#mappings
....
I've tried reading the content of the file using a test application I
wrote just to try to find the bug but I get the same problem. When it's
time for the program to read the #mappings line the debugger displays
the content of the line variables as ??? or a square symbol. If I
remove the path line in the middle of the two lines above everyting
works ok. I've tried using \\ and \ instead of / but that didn't help.
The strange thing is that if I unroll the loop and create something
like
ifstream file("levels\\level1.txt");
string line;
getline(file, line);
getline(file, line);
string line2;
getline(file, line2);
line2 will contain #mappings after the call to getline.
I'm using Visual Studio .NET 2003.
Any ideas?
/M 5 3617
On 1 Jan 2006 09:40:29 -0800 in comp.lang.c++, al**************@hotmail.com wrote, getline(file, line); while(!file.eof()){
Should be:
while(getline(file,line)) {
time for the program to read the #mappings line the debugger displays the content of the line variables as ??? or a square symbol. If I
The smarter a debugger is, the less I trust it. I would rather
trust "cerr <<" in the program.
remove the path line in the middle of the two lines above everyting works ok. I've tried using \\ and \ instead of / but that didn't help.
There should be nothing special about / or \ in file data.
As you are aware, \ is special between quotes in source code. al**************@hotmail.com wrote: Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating.
Inside the function that reads the content of the file there is a loop that basically looks like this
ifstream file("levels\\level1.txt"); string line; getline(file, line); while(!file.eof()){ ... getline(file, line); }
The first part of the level file looks like this
#configs levels/objectconfig.txt #mappings ....
I've tried reading the content of the file using a test application I wrote just to try to find the bug but I get the same problem. When it's time for the program to read the #mappings line the debugger displays the content of the line variables as ??? or a square symbol. If I remove the path line in the middle of the two lines above everyting works ok. I've tried using \\ and \ instead of / but that didn't help.
The strange thing is that if I unroll the loop and create something like
ifstream file("levels\\level1.txt"); string line; getline(file, line); getline(file, line);
string line2; getline(file, line2);
line2 will contain #mappings after the call to getline.
I'm using Visual Studio .NET 2003.
Any ideas?
/M
Try this:
std::ifstream file("levels\\level1.txt");
std::string line;
while(std::getline(file, line)){
// Do stuff with line...
}
Regards,
Peter Jansson
<al**************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... Inside the function that reads the content of the file there is a loop that basically looks like this
ifstream file("levels\\level1.txt"); string line; getline(file, line); while(!file.eof()){ ... getline(file, line); }
Here's a nice idiom for this kind of thing, which I learned from Walter
Brown:
ifstream file("levels\\level1.txt");
for (string line; getline(file, line); ) {
// whatever
}
Andrew Koenig <ar*@acm.org> wrote: <al**************@hotmail.com> wrote in message news:11**********************@g43g2000cwa.googlegr oups.com...
Inside the function that reads the content of the file there is a loop that basically looks like this
ifstream file("levels\\level1.txt"); string line; getline(file, line); while(!file.eof()){ ... getline(file, line); }
Here's a nice idiom for this kind of thing, which I learned from Walter Brown:
ifstream file("levels\\level1.txt"); for (string line; getline(file, line); ) { // whatever }
That is nice. I like that better than my usual idiom of
ifstream file("levels\\level1.txt");
string line;
while (getline(file, line)) {
// whatever
}
since it automatically makes line go out of scope when it's finished.
--
Marcus Kwok
On Fri, 6 Jan 2006 18:43:19 +0000 (UTC), ri******@gehennom.net (Marcus
Kwok) wrote: That is nice. I like that better than my usual idiom of
ifstream file("levels\\level1.txt"); string line; while (getline(file, line)) { // whatever }
Questions like the above frequently pop up in C++ newsgroups. People
don't understand what e.g. 'while (getline(file, line))' or 'while
(cin >> str)' means. The problem is that iostreams fail to provide
intuituive and consistent error handling (therefore error handling is
usually neglected in examples). A clearer 'idiom' would be:
while (getline(file, line) != NULL) {
// ...
}
if (!file.eof() || file.bad()) {
// error
}
Best wishes,
Roland Pibinger This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: SB |
last post by:
Hello. I have an input file which is laid out in the following manner...
Name
Day 1
am time 1
am time 2
appointment
pm time 1
pm time 2
appointment
Day 2
|
by: Skywise |
last post by:
I keep getting the following error upon compiling:
c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664:
'class istream &__thiscall istream::getline(char *,int,char)' : cannot
convert...
|
by: Eric Boutin |
last post by:
Hi all ! I'm currently writing a function that evaluate if there is a
diffence beetween 2 streams (istringstream and ifstream).
The problem is, it doesn't work. It *always* finds a difference...
|
by: Alex Vinokur |
last post by:
What is wrong with small_buffer in program below?
I/O getline doesn't read data from file into small (relative to file line size) buffer.
====== foo.cpp ======
#include <cassert>
#include...
|
by: Amadeus W. M. |
last post by:
I'm trying to read a whole file as a single string, using the getline()
function, as in the example below. I can't tell what I'm doing wrong.
Tried g++ 3.2, 3.4 and 4.0. Thanks!
#include...
|
by: abottchow |
last post by:
Hi all,
I'm new to the group and am seeking your advice on my Linux programming
problem.
Two programs are involved. One is myProgram.cc, which reads user's
input from keyboard and prints...
|
by: stevenruiz |
last post by:
Hi Everyone,
The problem that I have involves compiling two files. I've
received this error before and I understood that I was missing the
correct library. The error is as follows:
ld:...
|
by: ankit.kumar.agarwal |
last post by:
I am facing a problem with getline
I am reading a text file with a getline function the lines can have
'|' as separator.
everything works OK but in case if i have 2 delimitors in file '234||...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |