Skip to content

Instantly share code, notes, and snippets.

@ael-code
Created February 26, 2018 13:13
Show Gist options
  • Save ael-code/0d58da3da39ce14dbd86debf2ee8c0f4 to your computer and use it in GitHub Desktop.
Save ael-code/0d58da3da39ce14dbd86debf2ee8c0f4 to your computer and use it in GitHub Desktop.
test_IO
#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