I'm trying to write a program that is able to open a text file and split it so I can save it in two new ones to save files faster. But with the code I have now I'm not able to print the chars that I pick from the orignal file to the new ones.
In my text file I have the text "Dutch people are tall".
In my new files I want to get: File 1: Dthpol r tl File 2: uc epeaeal
This is the code I have got so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char cUsb1;
char cUsb2;
char str[128];
FILE *ptr_readfile;
FILE *ptr_usb1;
FILE *ptr_usb2;
ptr_readfile = fopen("Dutch People.txt","r"); // open readfile
while(ptr_readfile != NULL) // keep running while readfile != null
{
if (ptr_readfile != EOF) // keep running while readfile != eof
{
cUsb1 = fgetc(ptr_readfile); // to get a char out of the readfile
ptr_usb1 = fopen("USB1.txt", "w"); // create and open USB1 file
fwrite(cUsb1 , str , str , ptr_usb1); //writing get c to file
cUsb2 = fgetc(ptr_readfile); // to get a char out of the readfile
ptr_usb2 = fopen("USB2.txt", "w"); // create and open USB2 file
fwrite(cUsb2 , str , str, ptr_usb2); //writing get c to file
fclose(ptr_usb1); // closing the file
fclose(ptr_usb2); // closing the file
}
break; // to stop the while loop
fclose(ptr_readfile); // closing the file
}
return 0;
}
Many things are not quite right. You need to look carefully through the warnings reported by the compiler - enable all warnings if possible (e.g. "-Wall") - and resolve them all. Then single-step through your program with a debugger until it does something you didn't expect.
As a starting point, instead of:
fwrite(cUsb1 , str , str , ptr_usb1);
you might mean
fwrite(&cUsb1 , 1 , 1 , ptr_usb1);
There should be a warning for that line to tell you that you shouldn't try to pass cUsb1 (a char) as the first parameter of fwrite, as that parameter expects a pointer, i.e. an address of something. Use &cUsb1 to mean "the address of cUsb1".