Solved

Dynamic name for the output txt

Posted on 2006-10-28
9
268 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
 
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
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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.
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.

911 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

21 Experts available now in Live!

Get 1:1 Help Now