• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

C programming sample

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.
4 Solutions
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.

Jinesh KamdarCommented:
>> 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
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);
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now