Hi, I'm having some problems with this bit of code. I need to randomly open some files that have numbers as their filenames. i came up with this snippet to be able to open them from numbers 0 to 99. The code should randomly pick a number and try to open a file with that name, if the file does not exist, it lowers the range of numbers it can pick until reaching 10. I ran this on Ubuntu and it worked perfectly, but when I switch to Windows the loop never ends. If instead of using a random filename I use, for example filename="Synthesis//2.txt"; the program runs fine. i don't know why the code works on Ubuntu but not in Windows Vista. Any help is apreciated.

std::string Synthesis::itoa(int number)
{
     std::stringstream word;
     std::string output;
     word<<number;
     word>>output;
     return output;
}
int Synthesis::Load( int Level){

    int disponible=100; // las sintesis disponibles del 0 al 99 en ese level
    ifstream tempsynth;
    string filename;


    int SynthNumber;

         do {
         SynthNumber = rand() % (disponible)+ Level*100;
         filename="Synthesis//"+itoa(SynthNumber)+".txt"; //itoa is a custom funtion that return a string
         
         Log<<filename.c_str()<<"\n"; //this prints the filename into a file
         tempsynth.open(filename.c_str());//,ios::binary);
         

         if (!tempsynth && disponible>=10)
            disponible=disponible-3;   //como no van a haber 100 sintesis se baja la busqueda hasta que hallan
                                    }

         while (!tempsynth );

>>filename="Synthesis//"+itoa(SynthNumber)+".txt";

I suspect that is the line that is wrong. The result of that is "Synthesis//1.txt", which has two / characters, not one. \ is the escape character, not /. So filename="Synthesis\\"+itoa(SynthNumber)+".txt" would be correct. If you want to use / then ok but you only need one of them.

: DEBUG_IDEA:
I suggest you to call GetLastError() and see whether this is an Privilege problem
or not. Then we can pinpoint the problem.

>>filename="Synthesis//"+itoa(SynthNumber)+".txt";

I suspect that is the line that is wrong. The result of that is "Synthesis//1.txt", which has two / characters, not one. \ is the escape character, not /. So filename="Synthesis\\"+itoa(SynthNumber)+".txt" would be correct. If you want to use / then ok but you only need one of them.

No , I don't think this is a problem but didn't tried it,because see
Just go to the command prompt and try this .

C:\> notepad.exe C:\\boot.txt

C:\> notepad.exe C:\boot.txt

same file opens !
So it does not affects, more ideas !
if the acient dragon's trick works please acknowledge us too.

Ancient Dragon, no that's not problem. Like I mentioned, if I initialize

filename="Synthesis//2.txt";

the program runs ok. I did what you suggested anyway and I got the same problem.

NicAx64, how do I run that function? in what namespace or object does it lie? I don't know if that will work because the program doesn't exit out, it just loops forever.

Yup, you are right. That does work :)

It works like that. I believe that's the cross-platform way of writing file paths. It works on every system, even though it's not the default in some (like Windows). I think that when I call filename.c_str() or when I add the strings together, the program messes up the editing slightly in a way that is undetectable when I write it to a file. Has that happened to anyone, either one of those functions adding and escape or invisible character?

Ok so I was wrong about that (I tested it wih two / characters and it works.)

Try a simple program like this and see if it works or not

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string filename = "c://dvlp//test2//test2//";
    for(int n = 1; n < 100; n++)
    {
    std::stringstream str;
    str << filename;
    str << n;
    str << ".txt";
    std::cout << str.str() << '\n';
    }

}

O.k. so there's no problem with the name, I checked and the only escape character it had was \n but I added that later so it shouldn't affect anything. Any other suggestions are welcome.

Ancient Dragon, the program still won't recognize the files. It was a good try, thanks for the suggestion.

Sorry about the late.

Oky in POSIX there is a global variable that is declared in the errno.h
`errno` variable.

In Win32 the analogy is GetLastError() function.It is declared in windows.h
so you have to include it.
http://msdn.microsoft.com/en-us/library/ms679360%28VS.85%29.aspx


And when you have the visual studio debugger and you need to lookout the error
code you don't need to do that.Just open the watch window and type
'@err' there to get the error code. and '@err,hr' to get the error message.

more debugging tips here:http://www.highprogrammer.com/alan/windev/visualstudio.html

Since you are using the same ifstream object throughout the do/while -loop, you need to call tempsynth.clear() whenever .open() fails. Otherwise the object remains in fail state and makes no attempt to open any further files.

By the way, are you initializing the random number generator (i.e. calling srand() )?.

Thank you mitrmkar! That solved the problem. I will mark this as solved but if you know why it worked on Linux but not in Windows, could you tell me? Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.