[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dynamic name for the output txt

Posted on 2006-10-28
9
Medium Priority
?
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

656 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