I am having problems with copying txt files. I need to info from one file to another.
My code looks like this,
_tprintf (TEXT("%s\n"), FindFileData.cFileName);
memset(fileName, 0x00, sizeof(fileName));
_stprintf(fileName, TEXT("%s\\%s"), path, FindFileData.cFileName); //iegust
FILE *fptr = fopen(fileName, "r");//atver
fscanf(fptr,"%[^\n]",c); //iegust datus no faila
printf("Data from file:\n%s",a);
strcpy(a, c); //nokope datus
buffer2 = strtok (c, ","); //norada partraukumu un tadas lietas
while (buffer2) {
buffer2 = strtok (NULL, ",");
if(i<1){ printf("%s\n", c);}
i++;
while (buffer2 && *buffer2 == '\040'){
buffer2++;
// TODO ieliec iekavinas
}
}
And after that I use basic fputs().
My problem is that this code ignores new lines. It prints out fine, each string in it's own line, but that does not happen in file. (\n).
Your problem is that you just need to copy information from one file to another. So, why you don't use a simple solution to do it than your. I have a snipet code can solve your problem easily as shown below.
If I am wrong about your question, please give me advices.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Related
This question already has answers here:
copy file function in C
(2 answers)
Closed last year.
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
printf("\n Program to copy a file in another name: \n");
printf("Enter the source file name: ");
scanf("%s", fname1);
fptr1 = fopen(fname1, "r");
if (fptr1 == NULL){
printf("File does not found or an error occured when opening!!");
exit(1);
}
printf("\n Enter the new file name: ");
scanf("%s", fname2);
fptr2 = fopen(fname2, "w");
if( fptr2 == NULL){
printf("File does not found or an error occured when opening!!");
fclose(fptr1);
exit(2);
}
while(1){
ch = fgetc(fptr1);
if(ch == EOF){
break;
}
else{
fputc(ch, fptr2);
}
}
printf("The file %s copied to file %s succesfully.\n", fname1, fname2);
fclose(fptr1);
fclose(fptr2);
}
That is the code I copy a file. Actually, my purpose is to move a file to another directory so I was think first I should copy the file then delete the source file. I am also open to better solutions.
Re "open to better solutions", if the goal is just to get the file moved from within a C program, and not necessarily to directly program it instruction-by-instruction in C, then I'd just use popen(), something like...
int moveme(char *source, char *target) {
int status=0;
FILE *fp=NULL;
char command[999];
sprintf(command,"mv %s %s",source,target);
if ((fp=popen(command,"r")) != NULL) {
pclose(fp); status = 1; }
return (status);
} /* --- end-of-function moveme() --- */
And that'll likely be more efficient than anything you can directly program yourself, especially if it's a large file. The operating system will probably just change some directory entries. Won't even directly touch the file itself.
In this code whenever I write a new sentence, it replaces the previous sentence in a file that I put earlier. I want to not replace the previous sentence and also allow other sentences in that file line after line.
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
// creating file pointer to work with files
FILE *fptr;
// opening file in writing mode
fptr = fopen("file.txt", "w");
// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
Open the file in append mode.
fptr = fopen("file.txt", "a");
https://en.cppreference.com/w/c/io/fopen
void mergeFile(){
//allocate memory
char * firFile = (char*) malloc(MAX_SIZE);
char * secFile = (char*) malloc(MAX_SIZE);
char * conFile = (char*) malloc(MAX_SIZE);
char * buffer = (char*) malloc(MAX_SIZE);
char ch;
// get name of first file
printf("Enter name of first file: ");
__fpurge(stdin);
gets(buffer);
strcpy(firFile, FOLDER);
strcat(firFile, buffer);
//get name of second file
printf("Enter name of second file: ");
__fpurge(stdin);
gets(buffer);
strcpy(secFile, FOLDER);
strcat(secFile, buffer);
//get name of file will store
printf("Enter name of file which will store contents of two files : ");
__fpurge(stdin);
gets(buffer);
strcpy(conFile, FOLDER);
strcat(conFile, buffer);
//open 3 file with 'r' and 'w' mode
FILE * firPtr = fopen(firFile,"r");
FILE * secPtr = fopen(secFile, "r");
FILE * conPtr = fopen(conFile, "w");
//check 3 file NULL or not
if (firPtr == NULL) {
printf("Can not open %s file\n", firFile);
remove(conFile);
} else if (secPtr == NULL) {
printf("Can not open %s file\n", secFile);
remove(conFile);
} else if (conPtr == NULL){
printf("Can not open %s file\n",conFile);
}else{
// write all character in first file to file will store
// MAY NOT WORK
while ((ch = fgetc(firPtr)) != EOF)
fprintf(conPtr, "%c", ch);
// write all character in second file to file will store
// MAY NOT WORK
while ((ch = fgetc(secPtr)) != EOF)
fprintf(conPtr, "%c", ch);
printf("Two file were merged into %s file successfully\n!",conFile);
}
//clear all
free(buffer);
free(firFile);
free(secFile);
free(conFile);
fclose(firPtr);
fclose(secPtr);
fclose(conPtr);
}
I use fget to get character from file and write to another file, I work well when i use two file, one for read and one for store, But when i try to merge two file to another file, this code didn't work, no things in side contains file. I run this code in Netbeans 8.2, can you give me mistake from this code, thanks so much!
I'm trying to read a text file that will contain two lines, something like this:
18,3,4,c;19,3,5,D
19100,18,18;19102,3,2
and i want to store the first line in a string called Students and the second one into another string called Courses.
I have wrote this code but it stores one line only and i can't get it to work with the second line
FILE *fptr;
if ((fptr = fopen("program.txt", "r")) == NULL) {
printf("Error! opening file");
exit(1);
}
fscanf(fptr, "%[^\n]", Students);
fclose(fptr);
Can anyone help me with that? I'm a newbie to c and i can't get how to do so, Thank you in advance.
FILE *fptr;
char buffer[255] = {'\0'};
if ((fptr = fopen("program.txt", "r")) == NULL) {
printf("Error! opening file");
exit(1);
}
fgets(Students, sizeof(Students), fptr);
fgets(Courses, sizeof(Courses), fptr);
fclose(fptr);
This line fgets(Students, sizeof(Students), fptr); will start reading from the begginning of the file and store the first line to Students char array & then fgets(Courses, sizeof(Courses), fptr); will read the second line and store it into Courses char array.
Make sure that the size of Students & Courses is large enough to accommodate each line into them.
You may try fscanf() for the problem:
#include <stdio.h>
int main(void) {
char Students[100], Courses[100];
FILE *fp = fopen("program.txt", "r");
if (!fp) {
printf("File wasn't opened.\n");
return -1;
}
fscanf(fp, "%s \n", Students);
fscanf(fp, "%s", Courses);
printf("%s\n", Students);
printf("%s\n", Courses);
fclose(fp);
return 0;
}
My program.txt contains:
John_Doe
Mathematics
Sample Output
John_Doe // Students
Mathematics // Courses
Use something like:
fscanf(fptr, "%[^\n]\n%[^\n]]\n", Students,Courses);
Where you tell scanf() to read up to a new-line, read and discard the new line, then do it again.
This is my code which I have written so far
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
char quit[4] = "exit";
// char *filearray[100];
char filearray[100][14];
FILE **originalfilearray;
int counter = 0;
//Copy part
while(1){
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
break;
printf("Cannot open file %s \n", filename);
exit(0);
}
strcpy(filearray[counter], filename);
originalfilearray[counter] = fptr1;
counter+=1;
}
//Paste part
for (int i = 0; i < counter; i++)
{
printf("Enter the filename to open for writing for file %s\n", filearray[i]);
scanf("%s", filename);
fptr2 = fopen(filename, "w");
// Read contents from file
c = fgetc(fptr2);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(originalfilearray[i]);
}
printf("\nContents copied to %s\n", filename);
}
}
The problem occurs when I run the paste code the file is created but no content is pasted.
I have already tried reading many post regarding array of pointers of file. Some suggested to create originalfilearray variable with a single pointer some with double.
The major problem I guess is with the copy part.
Can someone please help me with the part where I need to copy the data of multiple files in the originalfilearray variable
Thank You
Apart from not allocating memory for originalfilearray, which other user explained, here are some things you are doing wrong
In
c = fgetc(fptr2);
You are trying to get character from an empty file you just opened in
fptr2 = fopen(filename, "w");
what you should be doing is starting a file pointer fptr and opening
FILE *fptr=fopen(filearray[i], "r");
and then copying content into it with
while ((c = fgetc(fptr))!= EOF)
{
fputc(c, fptr2);
}