how can i put the filename get in scanf("%s", &filename); in fopen("filename.txt", "a");

main()
{
   FILE *file;
   .
   .
   .
   printf("type the filename (.txt): ");
   scanf("%s", &filename);

   file = fopen("filename.txt", "a");

   fprintf(file, "...");

   fclose(file);
   .
   .
   .
}

Recommended Answers

All 17 Replies

don't use scanf() because it won't let you put spaces in the file name.

char filename[255]; // max possible length on MS-Windows
printf("Enter a file name\n");
fgets(filename, sizeof(filename), stdin);
// truncate the '\n' if it exists
if( filename[strlen(filename)-1] == '\n')
  filename[strlen(filename)-1] = 0;
file = fopen(filename, "a");
commented: Yes indeed +18

there is a max number of variables that i can declare?!
no, right?!

but my code its given a runtime error when a put 1 more variable
i tested.. run one time, all ok
then i declare a DIFERENT variable and run
then i give the error
i swear, i only declare one more variable =P
didnt change anything else

hm... there's something (else) wrong
it pass by directly...

printf("Enter a file name\n"); // here i suppose to type the filename
fgets(filename, sizeof(filename), stdin);

printf("...");

it prints like this...

Enter a file name
...

there is a max number of variables that i can declare?!
no, right?!

Well yes there is a max, but that was not what I meant. File path including file name can not exceed 255 characters. *nix is similar, but not sure what the max is.

hm... there's something (else) wrong
it pass by directly...

printf("Enter a file name\n"); // here i suppose to type the filename
fgets(filename, sizeof(filename), stdin);

printf("...");

it prints like this...

Enter a file name
...

It prints like that because that's exactly what you told it to do. Garbage in, garbage out.

Well yes there is a max, but that was not what I meant. File path including file name can not exceed 255 characters. *nix is similar, but not sure what the max is.

you're talk about the 255 in the variable filename
that i understand, but
i wasnt talking about that
when i declare char *filename[255]
an error occur... a runingtime error
before was working normaly
and the only thing that change was declare a new variable (char *filename[255])

i'm confuse

It prints like that because that's exactly what you told it to do. Garbage in, garbage out.

so... i dont know how to programme
for me that's right...

char *filename[255]

That declares an array of 255 POINTERS, not characters, because of the asterisk (star). Remove the star and it will work ok for you tool.

hm... there's something (else) wrong
it pass by directly...

printf("Enter a file name\n"); // here i suppose to type the filename
fgets(filename, sizeof(filename), stdin);

printf("...");

it prints like this...

Enter a file name
...

If you want to print the file name on the last line then do this: printf("%s\n", filename);

If you want to print the file name on the last line then do this: printf("%s\n", filename);

no... it was just an example
what i'm trying to say it's that it dont let me enter the filename

the '...' was to say that the code continue

when it prints 'Enter the filename'
its suppose to stop and wait until i enter the filename and press enter
then it continue
but this dosnt happen
it prints 'Enter the filename' and continue
dosnt wait for me to enter

That declares an array of 255 POINTERS, not characters, because of the asterisk (star). Remove the star and it will work ok for you tool.

i wrote here wrong, sorry
i declare char filename[255], not char *filename[255]
and recive the error

I think the reason that it didn't wait for you to enter the filename is because of something that was done earlier in the program, such as entering an integer of some sort. You need to post your whole program in order to determine what the problem is. I know for a fact that the problem is not fscanf().

I think the reason that it didn't wait for you to enter the filename is because of something that was done earlier in the program, such as entering an integer of some sort. You need to post your whole program in order to determine what the problem is. I know for a fact that the problem is not fscanf().

this problem i fixed \o/

now... it wont let me declare more variables :S

this problem i fixed \o/

now... it wont let me declare more variables :S

It's really best if you just post the code you are having problems with, explaining what the trouble is.

this problem i fixed \o/

now... it wont let me declare more variables :S

This is like going to a car repair shop without the car and asking them to fix it.

It's really best if you just post the code you are having problems with, explaining what the trouble is.

my code its too big
and its all in portuguese
it will take a lot of time to translate everything

Ancient Dragon

thanks for the help
i'll try to fix the rest of it

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.