How do I compile/open such a file - c++

I don't know how to compile this class, it exits with -1 when I compile it in visual/codeblocks. Do I have to use command line? How do I do that?
int main(int argc, char* argv[]) {
if( argc <= 1 )
return -1;
Chunk::init();
FILE *f = fopen(argv[1], "rb");
Chunk *obj = read_chunk( f );
fclose( f );
delete obj;
if( argc > 2 )
return 0;
OpenGL ogl(&argc,argv);
}
OpenGL ogl(&argc,argv);
}

As the code suggests, you have to supply a command-line option!
FILE *f = fopen(argv[1], "rb");
The command-line argument should be some file that will be open by this program. Since you only paste a snippet, I don't know what this program is for.
But say the compiled binary is a.exe, then you should run it as:
a.exe A_FILE_NAME

Related

File open issue, after file deleted. fopen status is failing in VC++

fopen example
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
FILE * pFile;
for(int i=0; i < 1000000; i++)
{
bool ret = remove("C:\\abc.txt");
pFile = fopen ("C:\\abc.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
else
{
printf("%d fopen() fails \n", count);
}
}
return 0;
}
Here, after remove is called, pFile = fopen ("C:\abc.txt","w");is called,
Sometimes even in C:\ the abc.txt file is not present after remove called, but the fopen pFile pointer is null.
This is reproduced some times not always. In this example this issue is reproduced 50/60 times.
Please suggest some solution.
If you have already established that is is a problem of race condition in the underlying file system, the correct way to fix it is (as suggested by BLUEPIXY) to simply remove the remove call :
pFile = fopen ("C:\\abc.txt","w");
will create the file if it does not exist and truncate it to 0 size if it exists, what is exactly what you need.
If you need to create a file surely after the file was removed, you can delay the fopen until you confirm that the old "abc.txt" file removed.
For that you need to introduce some loop to confirm it like below,
bool ret = remove("C:\\abc.txt");
FILE * rFile;
while(true)
{
rFile = fopen ("C:\\abc.txt","r");
if(rfile == null)
break; //File Removed confirmed!!
else
sleep(100); //Loop around again...
}
pFile = fopen ("C:\\abc.txt","w");
if (pFile!=NULL)

Checker the output sequence is correct or not

I am doing a homework , and have to check the result , but the problem is I dont know what i need to fill up .
What is the checker mean? if the input.txt ( inFile) is I 23 I 1 R 3 R 4 and the output.txt ( outFile) is 23 0 0 0
#include<cstdio>
//Check whether the output sequence is correct or not.
//If it is correct, return true. Otherwise, return false.
bool Checker(FILE* inFile, FILE* outFile)
{
//TODO
return false;
}
int main(int argc, char* argv[])
{
FILE* inFile = fopen(argv[1], "r");
FILE* outFile = fopen(argv[2], "r");
printf("%s\n", Checker(inFile, outFile)?"YES":"NO");
fclose(outFile);
fclose(inFile);
return 0;
}
Read both files into memory, and compare the memory.
Read about e.g. malloc, fseek, ftell, rewind, fread and memcmp.

Segmentation fault executing file writing using FILE pointer

I get a "Segmentation fault" for the following C++ code:
#include <cstdio>
int main(int, char**) {
FILE *fp;
fp = fopen("~/work/dog.txt", "w");
fprintf(fp, "timer, timer3, timer5, timer6, timer7");
fclose(fp);
}
Your path is invalid and will never work, thus fopen sets fp to NULL and you get a segfault. Hint: the ~ character is expanded by the shell, you can't use it in an argument to fopen.
A correct, safe implementation of what you're trying to do might look as follows. This is tested. It's also the reason why sane people don't write in C unless they have no other way of doing it :)
// main.cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
int main(int, char**)
{
const char * home = getenv("HOME");
if (home == NULL || strlen(home) == 0 || access(home, F_OK) == -1) abort();
const char * subPath = "/work/dog.txt";
char * path = (char*)malloc(strlen(home) + strlen(subPath) + 1);
if (path == NULL) abort();
strcpy(path, home);
strcat(path, subPath);
FILE *fp = fopen(path, "w");
if (fp != NULL) {
fprintf(fp, "timer, timer3, timer5, timer6, timer7");
fclose(fp);
}
free(path);
}
A few things:
you need to check fp for NULL before using it, else you'll get a segfault whenever the file isn't found.
you need to resolve the full path before passing it to fopen (fopen doesn't know what to do with "~")
example:
FILE *fp = NULL;
char path[MAX];
char *home = getenv ("HOME");
if ( home )
{
snprintf(path, sizeof(path), "%s/work/dog.txt", home);
// now use path in fopen
fp = fopen(path, "w");
if ( fp )
{
fprintf(fp, "timer, timer3, timer5, timer6, timer7");
fclose(fp);
}
else
{
std::cout << "your dog is missing" << std::endl;
}
else
{
std::cout << "You are homeless" << std::endl;
}
Segfault happens is the file you're trying to open does not exist. This has nothing to do with Qt.
Test for the nullity of 'fp' and handle the error correctly. Something like
FILE *fp = fopen("/path/to/work/dog.txt", "w");
if (fp == NULL)
{
printf("File does not exist.\n");
// throw exception or whatever.
}

Open file name passed from commandline argument that is in different location in the server

I want to open the filename name that I sent from command line but the file is in /home/docs/cs230 . Below is the code I tried but it shows error when I tried to compile in linux:
int main(int arg, char* args[1]) {
// Open the file
newfile = fopen("/home/docs/cs230/"+args[1], "w+b");
}
Since this is C++ we can use std::string like so:
int main(int arg, char* args[]) {
// Open the file
std::string path( "/home/docs/cs230/" ) ;
path+= args[1] ;
std::cout << path << std::endl ;
FILE *newfile = fopen( path.c_str(), "w+b");
}
Mats also makes a great comment that in C++ we would use fstream, which you can read more about at the link.
Since this is C++, I would suggest this:
int main(int argc, char *argv[])
// Please don't make up your own names for argc/argv, it just confuses people!
{
std::string filename = "/home/docs/cs230/";
filename += argv[1];
newfile = fopen(filename.c_str(), "w+b");
}
[Although to make it fully C++, you should use fstream, not a FILE
If you want to stick with pointers you can concat the string (char*)
const char* path = "/home/docs/cs230/";
int size1 = sizeof(argv[1]);
int size2 = sizeof(path);
const char* result = new char[size1 + size2 + 2];
result[size1 + size2 + 1] = '\0';
memcpy( result, path, size1 );
memcpy( &result[ size1 ], argv[1], size2 );
not a recommended option, but there are a bunch of possibilities here.

Is there an easy way to tell if a File stream has opened a directory instead of a file?

I'm making an HTTP server and when I get the path of the file they request I open it with the following:
returned_file = fopen(path, "r");
this (contrary to what I would think) succeeds even if the path is a directory. Is there an easy way to check if the returned_file stream is a directory instead of a file?
you can use fstat on the file descriptor returned by fopen.
Edit:
Here's and example program:
#include <sys/stat.h>
#include <stdio.h>
void printISDir( FILE* fp, char const * name ) {
int fdes = fileno(fp) ;
struct stat fileInfo ;
fstat(fdes, &fileInfo ) ;
if ( S_ISDIR(fileInfo.st_mode ) ) {
printf("%s: I'm a dir!\n", name ) ;
} else {
printf("%s: I'm a file!\n", name ) ;
}
}
int main( int argc, char** argv ) {
char const * directoryName = "/etc" ;
char const * fileName = "/etc/hosts" ;
FILE* dirFp = fopen(directoryName, "r") ;
FILE* fileFp = fopen(fileName, "r") ;
printISDir( dirFp, directoryName ) ;
printISDir( fileFp, fileName ) ;
fclose(dirFp) ;
fclose(fileFp) ;
return 0 ;
}
Elaborating on the other answers, you can call fstat on the returned file descriptor and check the st_mode for the S_IFDIR bit. The S_ISDIR helper macro is helpful:
#include <sys/stat.h>
...
FILE* f = fopen(path, "r");
struct stat buf;
if (fstat(fileno(f), &buf) == -1) {
perror("fstat");
} else {
if (S_ISDIR(buf.st_mode)) {
printf("is directory\n");
} else {
printf("not directory\n");
}
}
Can you check if the path points to a directory before you call fopen?
Use stat() on the file name before you open it, or fstat() the file descriptor fileno(returned_file).