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.
starviewAsked:
Who is Participating?
 
fridomCEO/ProgrammerCommented:
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
fclose(in)

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
fprintf(out, "%d", some_read_number);

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

Regards
Friedrich
0
 
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
0
 
abithCommented:
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");
      while(!feof(ip))
             putc(getc(ip), op);
      fclose(op);
      fclose(ip);
0
 
itsmeandnobodyelseCommented:
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);
    fclose(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);
    fclose(pfout);
    free(buf);

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
   
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.