This line is incorrect:
sprintf(filename,"%d.txt", stdin);
Since you've already gotten the input from stdin, you should be supplying the variable which contains the user input as an argument instead of stdin .
And since you read it in as a string, you'll want to change "%d" to "%s". Also beware of newlines in your string, which could thoroughly mess up your program.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Read my last sentance:
Also beware of newlines in your string, which could thoroughly mess up your program.
You're going to have to remove the newline character (if it's there), which is usually as simple as doing the following
if (string[strlen(string)-1] == '\n')
string[strlen(string)-1] = '\0';
Anyhow, the code works fine for me, even without the newline character removal. So perhaps it would be best for you to show us the code you're using?
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
the program runs, but no output file is generated? am i missing a command still?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
char line[255];
fgets(line, 255, stdin);
char filename[80]; // This line can't be here. It has to
// be above all executable code in C
sprintf(filename,"%s.txt",line);
return 0;
}
Yeah. Where do you:
1) open the file after you generate the name?
2) write to the file?
3) close the file before exiting?
:?:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
You still have to open the file for writing. It doesn't magically get created just because you create a name in a variable...
Look up fopen()
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
well i used
fout = fopen("%s.txt", "w");
but how do i represet "%s.txt" as the user's input?
you already posted the answer to that question in your post #5. Just use variable filename
sprintf(filename,"%s.txt",line); // <<< from your previous post
fout = fopen(filename, "w");
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
but i though the entry in fopen has to be a either in "" or predefined?
It can be, but it can also be a variable, such as I posted. All it is looking for isconst char*, doesn't care if it is a char variable or a literal string.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
fgets() function leaves the '\n' in the input buffer, so you need to truncate it
printf("enter student ID:");
fgets(line, 255, stdin);
// truncate '\n'
if( line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
sprintf(filename,"%s.txt",line);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
but i though the entry in fopen has to be a either in "" or predefined?
Why would you think that? ;)well i used
fout = fopen("%s.txt", "w");
but when i leave it as (filename,'w') and compile and run, the program notes a windows error and closes, and no file is generated
Why did you change"w" to 'w'? Change it back and it will work.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
/* You really shouldn't use goto's they're gay but what-the-hell */
End:
"Practice what you preach."
If you
1) know you shouldn't use them,
2) then say you shouldn't use them,
3) and do it anyway,
what are you teaching the people that are new to the language? It's confusing because for them, the code is now useless. They don't understand enough to know what to ignore and what is good... FYI... ;)
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Also, not to be rude, but the code posting was kind of pointless.The code was MAJORLY overcommented.
The fix the OP needed was just a matter of one or 2 lines.
There was nothing the OP needed to know in your post that hadn't already been shown already.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339