Thread: Need Help Urgently

  1. #1
    Registered User
    Join Date
    Jun 2002

    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

    thank you very much

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    You can use sprintf to write an int to a string.
    sprintf(String, "%d", Integer);
    it is very similiar to printf.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Jun 2002

    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@





    char *getFileName(int in){
    char *temp1="input";
    char *temp3=".IWP";
    char *temp2="";
    printf("temp2 is %s\n", temp2);
    strcat(temp1, temp2);
    strcat(temp1, temp3);
    printf("temp1 is %s\n", temp1);
    return temp1;


    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;





    /* data_name=getFileName(data);
    /* data_file=fopen(data_name,"r");
    /* current_file=fopen(current_name, "w+");

    /* 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

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    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"
    >fopen(INPUT_FILE, "r");

    (Oh, I see, just read your code a bit better, you want a number in the filename... )

    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:

    #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....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgently in need of help...!!!
    By patron in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 11:12 PM
  2. Need urgently help!
    By Frandy in forum Windows Programming
    Replies: 4
    Last Post: 03-05-2005, 08:02 AM
  3. Need help urgently
    By jstevanus in forum C++ Programming
    Replies: 15
    Last Post: 12-09-2004, 12:30 PM
  4. Help urgently needed, please =(
    By *Michelle* in forum A Brief History of
    Replies: 4
    Last Post: 06-03-2003, 07:14 AM