Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Opening a file

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I try to load a file with the line: std::ifstream file("\\Data\\Names.txt.c_str()"); Is this the wrong way to do it? I must use double \ otherwise cpp will think the char afterwards is an escapechar... also it should be converted to the c str style. What am i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Mizipzor
std::ifstream file("\\Data\\Names.txt.c_str()");


For one thing the .c_str() is inside the quotes therefore it thinks thats the file its opening. Its going to the file "names.txt.c_str()" which won't exist. Im not sure why your wanting to convert this to a c_str() ?

Share this post


Link to post
Share on other sites
Hmm... I just thought Ive read it in a tutorial or something somewhere :S maybe my memory misguides me. But

std::ifstream file("\\Data\\Names.txt");

Would be correct then? Assuming the txt file is located in a subfolder named Data which is at the same place as the cpp files?

Share this post


Link to post
Share on other sites
Yes, that would be correct. Assuming the place its running from is correct I know when you run it from the IDE itself, it will some what change the results of where its reading from.

I belive you can fix that by adding a "." in front of the first "\\" ; though thats only if the folder/file is like in the debug or release folder, and not where the cpp files are.

Share this post


Link to post
Share on other sites
The leading backslash indicates an absolute path. This is probably not what you want.

Share this post


Link to post
Share on other sites
[edit] Fruny: thats could be that problem... how is the proper way to do it then?

---------


Hmm... well the file seems to work ok since my temp string is showing the right things.

The two files are data for a kind of chatbot. Several names (many individuals) and some phrases. The first line is a number that says how many lines there is in the file (since i dont want to hardcode it if i want to add names). Same goes for phrases.

But I added that testOutput to see if it worked and it doesnt print anything. And im getting some quite nasty errors that I cant make ouy what they mean.

My class is only a constructor... so far... and the constructor loads those data. Can anyone see whats wrong?

NpcTraders.h

#ifndef _NPCTRADERS
#define _NPCTRADERS

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

using namespace std;

class NpcTraders {
public:
NpcTraders();
string requestMessage();

void testOutput();
private:
vector<string> name;
vector<string> phrase;

int numberOfTraders;
int numberOfPhrases;
};

NpcTraders::NpcTraders() {
this->numberOfPhrases = 0;
this->numberOfTraders = 0; // just in case

// First the names
std::ifstream file(".\\Data\\Names.txt");

if(file.bad() == true) { // if file didnt load, abort
cout << "Unable to load file \"Names.txt\"";
return;
}

file >> this->numberOfTraders;

string tempStr;
char temp[30];

while(!file.eof()) { // run until end is reached
file.getline(temp, 30, '\n');

name.push_back(string(temp));
}

file.close();

// Then the phrases
file.open(".\\Data\\Phrases.txt.c_str()");

if(file.bad() == true) { // if file didnt load, abort
cout << "Unable to load file \"Phrases.txt\"";
return;
}

file >> numberOfPhrases;

while(1) {
file.getline(temp, 30, '\n');

phrase.push_back(string(temp));
if(file.eof()) // if its the end of the file, break
break;
}

file.close();

this->testOutput();
}

void NpcTraders::testOutput() {
cout << "Name: " << name.at(0) << endl;
cout << "Phrase: " << phrase.at(0) << endl;
}

#endif




4 warnings:

Deleting intermediate files and output files for project 'Tradewars - Win32 Debug'.
--------------------Configuration: Tradewars - Win32 Debug--------------------
Compiling...
main.cpp
d:\program\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
d:\program\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,st
d::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std::basic
_string<char,std::char_traits<char>,std::allocator<char> > > &)'
d:\program\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic
_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
d:\program\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,st
d::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std::basic
_string<char,std::char_traits<char>,std::allocator<char> > > &)'
d:\program\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::~v
ector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
d:\program\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::ve
ctor<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Linking...

Tradewars.exe - 0 error(s), 4 warning(s)


Share this post


Link to post
Share on other sites
It seems like your using VC6 which is infamous for crappy template support/STL issues so you can safely ignore those warnings.

Share this post


Link to post
Share on other sites
Ive still got troubles loading data from that file. Ive changed to code to this:


// First the names
std::ifstream file("Names.txt");

if(file.bad() == true) { // if file didnt load, abort
cout << "Unable to load file Names.txt";
return;
}

file >> this->numberOfTraders;
cout << this->numberOfTraders;



The txt file is in the same directory as everything else... How come the output of that cout is 0 when the first line in that file read 10?

Share this post


Link to post
Share on other sites
As a summary:

- C++ string literals are of type char*, not std::string. style enthusiasts may consider this a bad thing, but it would be pretty difficult to do optimizations were it the other way around, and that would go against the grain of the language. Anyway, just use the literal directly as a parameter for the ifstream constructor; a .c_str() method call is only needed if you provide a *variable* of std::string type. And in no case (not in C++ anyway, unless you're writing an interpreter or something) would contents of a string ever be interpreted as a method call. :)

- Use forward slashes for path names, even on systems where that's not the file path separator. The C++ runtime is responsible for translating things appropriately. It avoids a need to escape the character (potential bug source, as illustrated), and gains you a tiny piece of portability for free.

- A leading backslash (or slash for that matter) indicates an absolute path. For a relative path, just omit that character.

- warning C4786 is generally safely ignorable; if you would prefer not to ignore it, the best solution is, well, get a better compiler. The warning message boils down to "wow! look at that unbelievably long typename! Yeah, *I* have trouble looking at it too! Damn this new-fangled C++ with its templates and such..." :)

- The current code looks ok. (Comparing with "== true" is not necessary and actually obscures readability, but is certainly not a problem for correctness.) What is the type of numberOfTraders? How are you verifying the file contents? (Is it, in fact, a text file?)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!