-
Need Help Urgently
I need to write a script which takes a input files (in ASCII format)
and output it into some format (ASCII) only change certain lines
ie line_num == 5/ 6 or 7...ect
The problem that I'm having now is that I can't write a interger to char which takes a interger and store it into string (ASCII format)
Thus, if you can help me, please e-mail me at [email protected]
thank you very much
-
You can use sprintf to write an int to a string.
Code:
sprintf(String, "%d", Integer);
it is very similiar to printf.
- Sean
-
I tried
Thanks Sean
I tried and I got segmant fault. I post my code below if any of you guys can tell me what's wrong I will be really appreciating.....@o@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char *getFileName(int in){
char *temp1="input";
char *temp3=".IWP";
char *temp2="";
sprintf(temp2,"%d",in);
printf("temp2 is %s\n", temp2);
strcat(temp1, temp2);
strcat(temp1, temp3);
printf("temp1 is %s\n", temp1);
return temp1;
}
main(){
FILE *data_file,*current_file,*temp_file;
/* data_file: source file */
/* current_file: the one for writing */
/* temp_file: only for testing purpose */
int data,current,name,test,line_num;
/* data: looping variable for data_file */
/* current: looping variable for current_file */
/* name: output from getFileName */
/* test: stores char from the source file */
/* line_num: the line numbers */
char *current_name, *data_name;
line_num=1;
for(data=0;data<99;data++){
current=data+1;
current_name=getFileName(current);
/* data_name=getFileName(data);
*/
/* data_file=fopen(data_name,"r");
*/
/* current_file=fopen(current_name, "w+");
fclose(current_file);
*/
/* fclose(data_file);
*/
}
}
i comment away all uncessary parts so know i only want to make sure getFileName works
the following is the output i received when i run the script
temp1 is input1.IWP
temp2 is 2
temp1 is input1.IWP2WP2
temp2 is 3
Segmentation fault
-
First off, your getFileName() function looks mighty complicated for simply returning a hardcoded filename. To simplify, it'd be better to use a #define. EG
>#define INPUT_FILE "myfile.txt"
then
>fopen(INPUT_FILE, "r");
(Oh, I see, just read your code a bit better, you want a number in the filename... :rolleyes: )
To point out the problems with your code:
>char *temp2 = "";
>sprintf(temp2, "%d", in);
Here temp2 is defined as a pointer to a const string that is zero in length. So when you do the sprintf, the length of memory you have to play with is only one byte. Also, because it's defined as a string const (""), it will be placed in read only memory on some OS's.
The same applies for *temp1.
Also, you are returning a pointer to a section of memory that will go out of scope as soon as you leave the function.
Here's an improved version:
Code:
#include <stdio.h>
#include <string.h>
#define MAXL_FILENAME 256
#define FILENAME "input"
#define FILENAME_EXT ".IWP"
void getFileName(char *buf, int in)
{
sprintf(buf, "%s%d%s", FILENAME, in, FILENAME_EXT);
}
int main(void)
{
FILE *data_file, *current_file, *temp_file;
int data, current;
char current_name[MAXL_FILENAME], data_name[MAXL_FILENAME];
for (data = 0; data < 99; data++)
{
current = data + 1;
getFileName(current_name, current);
printf ("name is %s\n", current_name);
}
return 0;
}
I left the function in there, but it's only one line long, so it'd probably be better to forget the function, and put the sprintf() in the loop in main().
Hope this helps....