Created
February 26, 2018 13:13
-
-
Save ael-code/0d58da3da39ce14dbd86debf2ee8c0f4 to your computer and use it in GitHub Desktop.
test_IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <cstring> | |
#include <memory> | |
using namespace std; | |
int main(int argc, char* argv[]) { | |
string p = "/tmp/mountdir/file"s; | |
char buffIn[] = "oops."; | |
char *buffOut = new char[strlen(buffIn)]; | |
/* Write the file */ | |
auto fd = open(p.c_str(), O_WRONLY | O_CREAT, 0777); | |
if(fd < 0){ | |
cerr << "Error opening file" << endl; | |
return -1; | |
} | |
auto nw = write(fd, buffIn, strlen(buffIn)); | |
if(nw != strlen(buffIn)){ | |
cerr << "Error writing file" << endl; | |
return -1; | |
} | |
if(close(fd) != 0){ | |
cerr << "Error closing file" << endl; | |
return -1; | |
} | |
/* Read the file back */ | |
fd = open(p.c_str(), O_RDONLY); | |
if(fd < 0){ | |
cerr << "Error opening file" << endl; | |
return -1; | |
} | |
auto nr = read(fd, buffOut, strlen(buffIn)); | |
if(nr != strlen(buffIn)){ | |
cerr << "Error reading file" << endl; | |
return -1; | |
} | |
if(strcmp( buffIn, buffOut) != 0){ | |
cerr << "File content mismatch" << endl; | |
return -1; | |
} | |
close(fd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment