Merging two files using C program

  • Thread starter Thread starter kthouz
  • Start date Start date
  • Tags Tags
    files Program
Click For Summary

Discussion Overview

The discussion centers around merging two files using a C program. Participants explore various methods and functions, including the use of pointers, file handling functions like fopen, and system calls. The conversation includes both theoretical and practical aspects of file merging.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about the role of pointers in merging files using C.
  • Another participant outlines basic steps for merging files using fopen, suggesting that pointers may not be necessary for these steps.
  • A request for a sample program is made by a participant unfamiliar with the fopen function.
  • A code snippet is provided demonstrating the use of fopen to write to a file.
  • Discussion includes alternative methods using Linux system calls like open(), read(), and write(), with mention of the O_APPEND flag for appending data.
  • One participant humorously questions the appropriateness of using OS commands for merging files instead of writing a C program.
  • A detailed code example is shared, highlighting a portable method for merging files, including error checking suggestions.
  • Another participant requests clarification on the provided code example for merging files.
  • A mention of using Windows-specific commands like CreateFile is made, noting potential portability issues.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of pointers and the appropriateness of using system calls versus standard C functions. The discussion remains unresolved regarding the best approach to merging files.

Contextual Notes

Some participants note the importance of error checking in file operations, and there are references to specific platform-dependent functions that may affect portability.

kthouz
Messages
188
Reaction score
0
How can i use c program two merge two files. Does it have to do with "pointers" or am going wrong!
 
Technology news on Phys.org
Open file "first" and file "second" in read mode using the fopen function; also open file "merged" in write mode using the fopen function; then read from files "first" and "second" and write what you read to file "merged"; then close all three files and you are done.

I'm sure there are more details involved that may or may not require pointers, depending on the exact nature of your assignment, but these basic steps do not use them.
 
Thanks for your answer. Just can you please give a me an example of a sample program to open another one because it's the first time i hear about fopen function. But don't worry tell me ill figure it out!
 
Code:
FILE * fp;

fp = fopen("filename.txt", "wt");

fprintf(fp, "Hello fopen\n");

fclose(fp);

See http://www.cplusplus.com/reference/clibrary/cstdio/fopen.html
 
What platform are you programming in? You can make use of the linux system calls - open(), read(), and write(). Open() has the O_APPEND flag that opens a file in append mode, thereby adding data to the end of the file. You can use this to merge your two files. Read the manual pages of the respective functions for a detailed description. Then again fopen() does have th "a" flag for append.
 
Last edited:
I don't imagine you're allowed to be so sneaky as to make an OS call, like "copy first+second merged" (DOS./cmd) or "cat first second > merged" (bash)?
 
This is a somewhat 'tedious' but portable way to do it in C.

Opening the primary file for 'update' rather than 'append' has the effect of removing the EOF marker before appending new data.


#include <stdio.h>
#include <stdlib.h>
#include <io.h>

#ifdef BYTE
#undef BYTE
#endif

#define BYTE unsigned char

BYTE *buf2;
long size;
FILE *fp1, *fp2;

//open file-1 for update, file-2 for read
fp1=fopen(fname1,"a+");
fp2=fopen(fname2,"rb");

//get size of file-2
fseek(fp2,0,SEEK_END);
size=ftell(fp2);
rewind(fp2);

//allocate buffer, read-in file-2
buf2=(BYTE *)malloc((size_t)size);
fread(buf2,size,1,fp2);

//write the buffer to (end of) file-1
fwrite(buf2,size,1,fp1);

//clean-up and close
free(buf2);
fclose(fp1);
fclose(fp2);

It'd be a good idea to do some error checking if you use this code for anything serious...
 
message

Give the program to merge two files into a single one ic C Programming
 
Dos command:

copy filein1/b+filein2/b fileout/b

If this is a windows console program, you might want to use CreateFile and related commands instead fopen, but the program would be less "portable" to other OS's.
 
  • #10
sunil bhatt said:
Give the program to merge two files into a single one ic C Programming

Which part of

morongo said:
This is a somewhat 'tedious' but portable way to do it in C.

Opening the primary file for 'update' rather than 'append' has the effect of removing the EOF marker before appending new data.


#include <stdio.h>
#include <stdlib.h>
#include <io.h>

#ifdef BYTE
#undef BYTE
#endif

#define BYTE unsigned char

BYTE *buf2;
long size;
FILE *fp1, *fp2;

//open file-1 for update, file-2 for read
fp1=fopen(fname1,"a+");
fp2=fopen(fname2,"rb");

//get size of file-2
fseek(fp2,0,SEEK_END);
size=ftell(fp2);
rewind(fp2);

//allocate buffer, read-in file-2
buf2=(BYTE *)malloc((size_t)size);
fread(buf2,size,1,fp2);

//write the buffer to (end of) file-1
fwrite(buf2,size,1,fp1);

//clean-up and close
free(buf2);
fclose(fp1);
fclose(fp2);

It'd be a good idea to do some error checking if you use this code for anything serious...


did you not understand?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
3K
Replies
81
Views
8K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K