Solved

Dynamic name for the output txt

Posted on 2006-10-28
9
277 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now