CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    70

    Exclamation Read File problem

    Hello, i am trying to read many different files. The filename has to change to different numbers. It loops 25 times.

    For example the first run i want it to ifstream myfile3 (npcs/npc1.txt); but the second run i want it to ifstream myfile3 (npcs/npc5.txt);

    so lets say...

    npcmapdata[1] = 1
    npcmapdata[2] = 5
    npcmapdata[3] = 35

    I tried this, but i get the error message '+' : cannot add two pointers. I have tried converting it to a string but then i get tons of errors. I really need some help on how to accomplish this. thank you very much.

    Code:
    while (npcloop < 25)
    {
    npcloop++;
    string account2 = "npcs/" + "npc" + npcmapdata2[npcloop] + ".txt";
    
    
    				  ifstream myfile3 (account2.c_str());
    				  if (myfile3.is_open())
    				  {
    					while (! myfile3.eof() )
    					{
    					  getline (myfile3,npc);
                                              etc etc
    }

    I know its very confusing, but basically i want my file reading loop to constantly change filenames to a number that i am holding in npcmapdata2 arrays.


    Thank you very much for any help i receive

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Read File problem

    char buffer[300];
    sprintf(buffer, "npcs/npc&#37;d.txt", npcmapdata2[npcloop]);
    string account2 = buffer;

    or:
    sprintf(buffer, "%s%s%d%s", "npcs/", "npc", npcmapdata2[npcloop], ".txt");

  3. #3
    Join Date
    Sep 2008
    Posts
    70

    Re: Read File problem

    Thank you so very much. Also the fast response was great .

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: Read File problem

    I think it would look a lot nicer (and be safer) if it was done in c++.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured