質問

Currently I am able (I think) to open a file using fopen. For testing purposes I want to be able to pass the file's contents to an output file, but I'm not getting the desired results. Here is some code:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]){ //practice on utilizing IO
char filepath[100];
char filepath2[100];
strcpy(filepath,"./");
strcpy(filepath,"./");
char typed[90];
char msg[1024];
FILE * new_file;
FILE * old_file;

// printf("Input file to be created\n");
printf("File to be opened as input: \n");
printf("--->");

fgets(typed,90,stdin); //read in file name
strtok(typed, "\n");
strcat(filepath,typed);
old_file =  fopen(filepath, "r");

printf("file to output to: \n");
fgets(filepath2,100, stdin);  
strtok(filepath2, "\n");
///attempt to create that file
new_file = fopen(filepath2,"w");
//printf("%s\n", msg);

}

Any help is appreciated.

役に立ちましたか?

解決

Opening a file-handle in a program is a little different than opening a document in a Word Processor. It's more like opening a book. To read or write you have to use your eyes (consuming the data) or a pencil (producing the data).

Since you have the files open, you need to read data from the first file and write it to the second file. Something like:

size_t nread;
do {
    nread = fread(msg, 1, 1024, old_file);
    fwrite(msg, 1, nread, new_file);
} while(nread != 0);

Or

int nread;
do {
    nread = fgets(msg, 1023, old_file);
    fputs(msg, new_file);
} while (nread > 0);

Or even just a char at a time.

int c;
while ( (c=fgetc(old_file)) != EOF) {
    fputc(c, new_file);
}

Also, you're not prepending the "./" to the second file. Not sure if that's important, but you did it to the first file.

Also, you should use fopen on new_file. freopen isn't wrong per se, but it's weird and will confuse others (including me).

The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen() function. src:manpage

So, it will open the stream as you wish, but it destroys stdout when it does this. So your program cannot do normal output anymore. This may not be important, but then there doesn't appear to be any real advantage either.

他のヒント

I created a small c file similar to yours, hopefully it helps you gain a better understanding of i/o in C.

Code:

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

int main(int argc, char *argv[]) {

char fileNameOne[] = "test.txt";
char fileNameTwo[] = "new.txt";

FILE* oldFile;
FILE* newFile;

printf("The file being read from is: %s\n", fileNameOne);
//get a File pointer to the old file(file descriptor)
oldFile = fopen(fileNameOne, "r");

//get a File pointer to the new file(file descriptor)
//a+ is to open for read and writing and create if it doesnt exist
//if we did not specify a+, but w+ for writing, we would get an invalid file descriptor because the file does not exist
newFile = fopen(fileNameTwo, "a+");
if (newFile == 0) {
    printf("error opening file\n");
    exit(1);
}
//read everything from the old file into a buffer
char buffer[1024];

printf("Contents of old file:\n");
while (fread(buffer, 1, 1024, oldFile) != 0) {
    //if fread returns zero, and end of file has occured
     printf("%s", buffer);
     //directly write the contents of fileone to filetwo
     fwrite(buffer, 1, 1024, newFile);
}

printf("The file %s has been read and written to %s\n", fileNameOne, fileNameTwo);

printf("Verification: Contents of file two:\n");

//move the offset back to the beginning of the file
rewind(newFile);

while (fread(buffer, 1, 1024, newFile)!= 0) {
    printf("%s", buffer);
}

printf("\nEnd of file 2\n");
}

I made a text file called test in the same directory and just wrote some garbage to it. This is the output.

Output:

The file being read from is: test.txt
Contents of old file:
Hi, my name is Jack!

HAHAHAH

YOU DON"T even know!


The file test.txt has been read and written to new.txt
Verification: Contents of file two:
Hi, my name is Jack!

HAHAHAH

YOU DON"T even know!



End of file 2
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top