writing a file

Dear All expert,

Can anyone show me a sample source code in C programming. To create a file and write to a file and save it once exist. Thanks a lot for all your help

Comments

  • : Dear All expert,
    :
    : Can anyone show me a sample source code in C programming. To create a file and write to a file and save it once exist. Thanks a lot for all your help
    [purple]
    /*The following will open the file named "File" in writing mode. if the file File exists, then all contents will be erased upon opening the file. if the file doesnt exist, then it will be created.
    */
    FILE *file = fopen("File", "w");

    /*Writing the character 'A' */
    fputc('A', file);

    /*Writing the string "Programming in C" */
    fputs("Programming in C", file);
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • : : Dear All expert,
    : :
    : : Can anyone show me a sample source code in C programming. To create a file and write to a file and save it once exist. Thanks a lot for all your help
    : [purple]
    : /*The following will open the file named "File" in writing mode. if the file File exists, then all contents will be erased upon opening the file. if the file doesnt exist, then it will be created.
    : */
    : FILE *file = fopen("File", "w");
    :
    : /*Writing the character 'A' */
    : fputc('A', file);
    :
    : /*Writing the string "Programming in C" */
    : fputs("Programming in C", file);
    : [/purple]
    [green]/* Good practice to close */
    fclose(file);[/green]
  • : : [/purple]
    : [green]/* Good practice to close */
    : fclose(file);[/green]
    :


    not only good practice, but in C its is absolutely imperative to avoid running out of FILE handles if the program opens files often during the same session.
  • Dear All Expert,

    Can i just write to the same file name without overwriting it? That means, everytime i open my application to write to the file, i will open the same file name and continue to write to the same file maybe starting in a new line.

    Thanks all for your respond

    Regards
  • : Dear All Expert,
    :
    : Can i just write to the same file name without overwriting it? That means, everytime i open my application to write to the file, i will open the same file name and continue to write to the same file maybe starting in a new line.
    :
    : Thanks all for your respond
    :
    : Regards
    :

    yes, of course you can. Just read the description in fopen() for the various open flags and choose the combination that does what you want it to do.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp

  • : Dear All Expert,
    :
    : Can i just write to the same file name without overwriting it? That means, everytime i open my application to write to the file, i will open the same file name and continue to write to the same file maybe starting in a new line.
    :
    : Thanks all for your respond
    :
    : Regards
    :
    There are many options to choose from when opening a file. If you want to insert data starting from a new line in the file, open the file in read mode, and another temporary file in write mode. Read from the first and write to the second until you get to the point you want to insert, then just write to the second the new data. After that read from the first and write to second until all is done.

    Then delete the first file and rename the second as the original. I use this scheme often when writing linked lists to a file. That way the elements of the list are stored sequentially on the disk which shortens up the code that reads them into memory a bit.
  • Hi all EXPERTS,

    I got the same question too. But this time there is an embedded SQL on it. The code is shown below.

    FILE *first;
    first = fopen("les5_160_insis_without_CD_allocated_20120214.csv","w+");
    fputc(Exec_SQL_1(), first);
    fclose(first);


    void Exec_SQL_1(void)
    {
    connectDB("ids_db");
    EXEC SQL
    SELECT company_code,
    sub_name,
    company_type,
    industry,
    product_services,
    contact_person_title,
    contact_person,
    designation,
    email,
    url,
    hse_desc,
    inst_blk_hse,
    inst_st_code,
    inst_st_level,
    inst_stunit,
    inst_bldg_code,
    address,
    postal_code,
    country,
    region,
    city,
    state,
    locality,
    tel,
    fax,
    operator,
    source
    FROM insis_phonebk_entitlemt E
    WHERE E.company_type =: P3
    AND E.company_code NOT IN ( SELECT company_code,
    FROM insis_phonebk_distribution D
    WHERE D.company_code = E.company_code
    AND D.issue_year =: D.issue_month =:P2);
    }


    Now, the question is this:

    Upon retrieving the embedded SQL, how can I write the retrieved data into the file???
  • Hi all EXPERTS,

    I got the same question too. But this time there is an embedded SQL on it. The code is shown below.

    FILE *first;
    first = fopen("les5_160_insis_without_CD_allocated_20120214.csv","w+");
    fputc(Exec_SQL_1(), first);
    fclose(first);


    void Exec_SQL_1(void)
    {
    connectDB("ids_db");
    EXEC SQL
    SELECT company_code,
    sub_name,
    company_type,
    industry,
    product_services,
    contact_person_title,
    contact_person,
    designation,
    email,
    url,
    hse_desc,
    inst_blk_hse,
    inst_st_code,
    inst_st_level,
    inst_stunit,
    inst_bldg_code,
    address,
    postal_code,
    country,
    region,
    city,
    state,
    locality,
    tel,
    fax,
    operator,
    source
    FROM insis_phonebk_entitlemt E
    WHERE E.company_type =: P3
    AND E.company_code NOT IN ( SELECT company_code,
    FROM insis_phonebk_distribution D
    WHERE D.company_code = E.company_code
    AND D.issue_year =: D.issue_month =:P2);
    }


    Now, the question is this:

    Upon retrieving the embedded SQL, how can I write the retrieved data into the file???
  • Hi all EXPERTS,

    I got the same question too. But this time there is an embedded SQL on it. The code is shown below.

    FILE *first;
    first = fopen("les5_160_insis_without_CD_allocated_20120214.csv","w+");
    fputc(Exec_SQL_1(), first);
    fclose(first);


    void Exec_SQL_1(void)
    {
    connectDB("ids_db");
    EXEC SQL
    SELECT company_code,
    sub_name,
    company_type,
    industry,
    product_services,
    contact_person_title,
    contact_person,
    designation,
    email,
    url,
    hse_desc,
    inst_blk_hse,
    inst_st_code,
    inst_st_level,
    inst_stunit,
    inst_bldg_code,
    address,
    postal_code,
    country,
    region,
    city,
    state,
    locality,
    tel,
    fax,
    operator,
    source
    FROM insis_phonebk_entitlemt E
    WHERE E.company_type =: P3
    AND E.company_code NOT IN ( SELECT company_code,
    FROM insis_phonebk_distribution D
    WHERE D.company_code = E.company_code
    AND D.issue_year =: D.issue_month =:P2);
    }


    Now, the question is this:

    Upon retrieving the embedded SQL, how can I write the retrieved data into the file???
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories