Solved

Dynamic name for the output txt

Posted on 2006-10-28
9
284 Views
Last Modified: 2010-04-15
something=fopen("result.txt","a");

it creates result.txt, but i want to create if the input file is input1, output file is input1's result. input2-input2's result ... ect

i use

main(int argc, char *argv[] )

so, i need smth like this

something=fopen("argv[]'s result", "a" );

argv will be input1.. like ( %d, integer )

so different txt files will be created according to input file when every time the program executed.

thanks.
0
Comment
Question by:thefirstfbli
  • 4
  • 4
9 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 17825214
hi

i did not quit understand what you are trying to acomplish, but you can pass the filename to fopen using a variable, so you can do something like this :
(assuming your input file is called inputXXX)

main (int argc, char* argv[])
{
int file_seq;
char output_filename[30];

sscanf(argv[1],  "input%d", &file_seq);
sprintf(output_filename,  "output%d",  file_seq);

something = fopen(output_filename, "a")


that's it

momi sabag
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17825298
it looks like true.

but

sscanf(argv[1],  "input%d", &file_seq);
printf("seq is %d", file_seq);
sprintf(output_filename,  "output%d",  file_seq);

something = fopen(output_filename, "a")

it prints correct file_seq but not correct output..

if input input1, output must be output1 but still only output.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17825719
> but i want to create if the input file is input1, output file is input1's result. input2-input2's result ... ect
#include <stdio.h>
#include <stdlib.h>

int main(){
char buffer[128] = { 0 };

_snprintf (buffer, 128, "%s%s", "hello.txt", "'s result"); // use snprintf for *nix

printf ("name of output file is %s\n", buffer);
}

ouput
name of output file is hello.txt's result
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:thefirstfbli
ID: 17826249
>ouput
  name of output file is hello.txt's result

yes it is correct one i think according to my first question, but momi_sabag's code looks better if it works,actually i want this, beacuse if input file name is inp1, the ouput file name will be out1.. ip2---->out2 ... ect

until

main (int argc, char* argv[])
{
int file_seq;
char output_filename[30];

sscanf(argv[1],  "input%d", &file_seq);
sprintf(output_filename,  "output%d",  file_seq);

it is acceptable, but it did not create correct output..

something = fopen(output_filename, "a");
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17826264
It should work fine

int main(){
char buffer[128] = { 0 };
int seq;
char * in = "input1";

sscanf (in,"input%d",&seq);

_snprintf (buffer, 128, "output%d", seq);

printf ("name of output file is %s\n", buffer);
}

Output
name of output file is output1

What was the error when you tried the code?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17826268
Also note that using this method, you are forced to provide input file names as input1, input2 etc. while your original question would have handled any file name by appending " 's results" to the input file name
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17826320
yours and other actually works, but i want to see the output file "output1" which comes from "input1" in my folder. no error, but same output name comes,

if user starts the program with this line:   ./program_name inp1
the output txt file must be like this : out1...

may be i did not understand beacuse i really tired.. sorry..
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17826333
Check return value of fopen
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17826357
oow yes.. thanks... corrected .. wow..
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question