Linux File Read and Write - C++ [Updated] -
i supposed create program reads source.txt's first 100 characters, write them in destination1.txt, , replace "2" "s" , write them destination2.txt. below code
#include <sys/types.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <cstdio> #include <iostream> using namespace std; int main(int argc, const char* argv[]){ argv[0] = "source.txt"; argv[1] = "destination1.txt"; argv[2] = "destination2.txt"; int count=100; char buff[125]; int fid1 = open(argv[0],o_rdwr); read(fid1,buff,count); close(fid1); int fid2 = open(argv[1],o_rdwr); write(fid2,buff,count); close(fid2); //how change characters? return 0; }
thanks guys able copying. how perform character replacement? if it's fstream
know how loop. i'm supposed use linux system calls.
define array out_buf , copy buff out_buf character character, replacing 2's s.
... read(fid1,buff,count); close(fid1); char out_buf [125]; int i; (i = 0; < sizeof (buf); i++) { if (buff [i] == '2') out_buf [i] = 's' else out_buf [i] = buff [i] } int fid2 = open(argv[1],o_rdwr); write(fid2, out_buf,count); close(fid2); return 0;
Comments
Post a Comment