Jump to content

Reading text file c++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
oogie

oogie

    Newbie

  • Members
  • Pip
  • 7 posts
Level = fopen ( "1.txt", "r" );


At the moment I am using this to open up a text file called '1.txt'. But I was wandering if there is anyway I could allow the name of the text file to be 1 of the variables in my program. So, for example if a variable was stored as level with the number 3 in it, it would open "3.txt".

#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
int level = 1;
char* filePath = malloc(100);
itoa(level, buffer, 10); // The third argument is the radix. If you want base 10, just use 10.
strncat(filePath, ".txt", 4); // Add .txt to the end of the file name
printf(filePath);
There you are. I used C functions btw, just because I feel that their easier.

Edit: Typo on the second line. Changed char to char*

Edited by brownhead, 18 April 2009 - 04:31 PM.


#3
oogie

oogie

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks