C programming sample

Posted on 2007-09-28
Last Modified: 2013-11-13
I want to open and read a file like "model-number.txt" which has a list of model numbers like 3033, 4523, 389, 67, 3233 and so on and place it model number into a string variable.
Once I have read all the model numbers. I want to open a new text file and write these model numbers to it.
Can some one show me an exsample C  not C++ source code how to do this.

I am just learn c, I learn better on exsamples.
Question by:starview
    LVL 24

    Accepted Solution

    Well you open a file for reading like this:

    FILE *in = fopen(file_name, "r");

    You read in lines either with scanf or fgets. What you choose depends on the format in the file. fgets is better if you expec quite some variation.
    reading in look like this
    char buf[SOME_LIMIT];
    char *pc,

    pc = fgets(buf, SOME_LIMIT, in);
    /* in is the FILE* from above */

    closign the file is done with fclose

    Opening for writing is again a call to fopen with slightly different parameters

    FILE *out = fopen(out_file_name, "w");

    writing can be done with
    fprintf(out, "%d", some_read_number);

    closing is of the out file is done with fclose again.

    LVL 18

    Assisted Solution

    by:Jinesh Kamdar
    >> Once I have read all the model numbers. I want to open a new text file and write these model numbers to it.
    Instead, you could open both files simultaneously - the source and the target files - and then loop to read the source file and write into the target file. In essence, you are trying to simulate the file-copy program using file-I/O operations of C programming language.

    1. Open the source file in read mode using fopen
    2. Open the source file in write mode using fopen
    3. Loop below till source file reaches EOF
        a. Read a no. from the source file into a local variable
        b. Write the no. held by the local variable into the target file
    4. Close the source file using fclose
    5. Close the target file using fclose
    LVL 5

    Assisted Solution

    if you are not going to process anything between reading and writing you can write as below

          FILE *ip = fopen ("c:\\tmpip.txt","r");
          FILE *op = fopen ("c:\\tmpop.txt","w");
                 putc(getc(ip), op);
    LVL 39

    Assisted Solution

    if you want to copy the inputfile you simply could read it with one read and write it with one write back.

    #include <stdio.h>
    #include <sys/stat.h>

        char inputfile[]   = "model-numbers.txt";
        char outputfile[] = "model-numbers_1.txt";
        struct stat fs = { 0 };
        FILE * pfin   = NULL;
        FILE * pfout = NULL;
        char* buf = NULL;
        int bytesread = 0;
        if (stat(inputfile, &fs) != 0)
              return 1;  /* file doesn't exist */
        pfin = fopen(inputfile, "br");
        if (pfin == NULL)
              return 2;
        buf = (char*)malloc(fs.st_size*sizeof(char));
        bytesread = fread(buf, sizeof(char), fs.st_size, pfin);
        /* here you could parse the buf for model numbers, e. g. by using strtok */

        pfout = fopen(outputfile, "wb");
        if (pfout == NULL)
              return 2;
        fwrite(buf, sizeof(char), fs.st_size, pfout);

    Note, testing an inputfile by stat gives some other useful information in struct stat, e. g. file times or whether the inputfile is a file or a directory.

    Regards, Alex

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    Fix45 challenge 15 65
    scores100 challenge 3 66
    wordsWithout 49 64
    allStar challenge 1 26
    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…
    This article will show, step by step, how to integrate R code into a R Sweave document
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now