Getting strings with /'s and .'s

Started by
19 comments, last by OneMoreToGo 17 years, 10 months ago
For some reason I'm unable to get a string from a file if it contains slashes and periods. I can load this: /this/is/a/string Or this: this.is.a.string But not this: /this/is/a.string When I do I get a memory segmentation fault error. (Code 11). I got that once when I tried closing a file stream that wasn't actually open yet. I'm not sure how the two are related besides using the Ifstreams and ofstreams. What could be going wrong? Thanks for any help! :)
Advertisement
Can we see the code that is causing the problem?
most certainly there is an error with your test code. the underlying mechanisms responsible for file IO aren't to blame here. please post some code that you're trying to read the strings with. especially make sure the buffers you're reading into are included in the snippet.
Sorry it took me so long to get back. Here's the main file:

Quote:
#include <iostream>
#include "flx_fileIO.h"

using namespace std;

int myName = 35;
int myName2 = 468;
char myString[15];
char myStringIn[127];

int main (int argc, char * const argv[]) {
FLX_purgeFileArray();
cout << "Purged array." << endl;
FLX_loadFile("file.txt", "r", myName);
cout << "Loaded file." << endl;
FLX_getString(myName, myString);
cout << "Retrieved string." << endl;
FLX_closeFile(myName);
cout << "Closed file." << endl;
cout << myString << endl;

/*
FLX_loadFile("file.txt", "w", myName2);
cout << "Input text: " << endl;
cin >> myStringIn;
FLX_writeInt(strlen(myStringIn), myName2);
FLX_writeString(myStringIn, myName2);
FLX_closeFile(myName2);
*/

return 0;
}



The FLX functions, which you will probably not be familiar with are part of FLX, a library I am making to simplify the stratedgy game development process. My output gets all the way to "Loaded File", which means it's the get string function that's tripping up.

Here's the body of the FLX_getString function.

Quote:

int FLX_getString(int flx_gs_name, char flx_gs_userWordSlot[]) {

// If this name is invalid.
int flx_gs_thisSlot = FLX_locateFileSlot(flx_gs_name);
if (flx_gs_thisSlot == -1) {
return(-1);
}
// If this name is for an empty or write slot.
if (flx_fileStreams[flx_gs_thisSlot].type[0] == ("e" || "w")) {
return(-1);
}

// And if we're ok...
flx_fileStreams[flx_gs_thisSlot].ifs >> flx_gs_userWordSlot;

return(0);
}



I should probably explain how FLX's file manager works. It uses array's for everything, and lets the user give these slots names. The use is able to manipulate that file slot with the name code. It's just like standard c++ i/o, I added these functions as an exercise to improve my file in/out understanding and to make FLX a more "all-inclusive" package.

I see nothing that could be having trouble here... I'll try using normal c i/o stuff.

Thanks!
Well, you are certainly not simplifying anything by using C constructs. Instead of character arrays, use std::string. Instead of what I can only assume is a raw array, use std::vector/std::list (or, considering you want named "file slots," use std::map).

I am certain that you are overwriting an array boundary, whether it is in one of your strings, or in your "file manager," or somewhere else. I cannot say without the relevant code.


jfl.
Quote:Original post by jflanglois
Well, you are certainly not simplifying anything by using C constructs. Instead of character arrays, use std::string. Instead of what I can only assume is a raw array, use std::vector/std::list (or, considering you want named "file slots," use std::map).

I am certain that you are overwriting an array boundary, whether it is in one of your strings, or in your "file manager," or somewhere else. I cannot say without the relevant code.


jfl.


Oh please no, I have no desire to go through all the code from my file manipulation and changing it all to more of c++'s strange formats. I use struct's because they allow me to decide what properties each file has, rather than having slots in an array for every aspect, which would be annoying and require changing loads of code which I do not wish to go through again.

I've tried using cin to get characters to arrays and then output with /'s and periods. I then did so with file in manipulators. All my function does is manage an array of structs doing the exact same thing. I'm guessing somehow structs are not allowed to contain .'s and /'s, although that seems unlikely, I'm pretty confident that my code is not causing this.

Oh wait, I just realized. Damn I'm dumb smurf... my array was too small. :(

Really sorry for this! *embarrassed* Yeah... I feel reeaallly stupid right now.
Quote:Original post by OneMoreToGo
Oh please no, I have no desire to go through all the code from my file manipulation and changing it all to more of c++'s strange formats. I use struct's because they allow me to decide what properties each file has, rather than having slots in an array for every aspect, which would be annoying and require changing loads of code which I do not wish to go through again.


They're not "strange formats". (Besides which, you're already using the iostream library just fine.) They're carefully designed classes and functions created by very smart people *specifically to avoid problems like*

Quote:Oh wait, I just realized. Damn I'm dumb smurf... my array was too small. :(

Really sorry for this! *embarrassed* Yeah... I feel reeaallly stupid right now.


Heck, the fact that I see this in your code:

if (flx_fileStreams[flx_gs_thisSlot].type[0] == ("e" || "w")) {


tells me you're not qualified to reinvent the wheel. (Hint: it doesn't do what you think it does. "e" || "w" simplifies to 1, and anyway, assuming that 'type' is a char**, you can't just compare strings like that: the comparison would compare the char* pointer values, not the pointed-at data.)

I'll make you an offer you can't refuse: PM me the whole flx_fileIO module (or instructions for getting it - just how big is that anyway o_O), and I'll rewrite it for you free, over the weekend, to the best of my ability.
Quote:Original post by Zahlman
Quote:Original post by OneMoreToGo
Oh please no, I have no desire to go through all the code from my file manipulation and changing it all to more of c++'s strange formats. I use struct's because they allow me to decide what properties each file has, rather than having slots in an array for every aspect, which would be annoying and require changing loads of code which I do not wish to go through again.


They're not "strange formats". (Besides which, you're already using the iostream library just fine.) They're carefully designed classes and functions created by very smart people *specifically to avoid problems like*

Quote:Oh wait, I just realized. Damn I'm dumb smurf... my array was too small. :(

Really sorry for this! *embarrassed* Yeah... I feel reeaallly stupid right now.


Heck, the fact that I see this in your code:

if (flx_fileStreams[flx_gs_thisSlot].type[0] == ("e" || "w")) {


tells me you're not qualified to reinvent the wheel. (Hint: it doesn't do what you think it does. "e" || "w" simplifies to 1, and anyway, assuming that 'type' is a char**, you can't just compare strings like that: the comparison would compare the char* pointer values, not the pointed-at data.)

I'll make you an offer you can't refuse: PM me the whole flx_fileIO module (or instructions for getting it - just how big is that anyway o_O), and I'll rewrite it for you free, over the weekend, to the best of my ability.



Type, is a char[0], not a pointer. It is not taken as an argument to a function, but is accessed directly from multiple different functions, and in no way by the user. If they try to I'll slap them. ;) I'd love to take up your offer though. And believe me, you'd get a large credit in the end, that's for sure. :)

I'll PM you a link to the code.
btw,

"e" != 'e'

one is a string constant ("e") and the other is a character literal ('e'). One has an ascii value of something like 101 (that's the 'e'), and the other has some 32-bit address (I'm assuming a 32-bit system)
BTW,

Before you read any of the code I send back to you, try to write any more code, or in any way represent that you know anything about C or C++, you need to read this and understand it in full.

This topic is closed to new replies.

Advertisement