Solved

Dynamic name for the output txt

Posted on 2006-10-28
9
259 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Check return value of fopen
0
 
LVL 1

Author Comment

by:thefirstfbli
Comment Utility
oow yes.. thanks... corrected .. wow..
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

10 Experts available now in Live!

Get 1:1 Help Now