I made a this file pointer in my code that should contain my log.txt file's address:
FILE *log
This address depends on the argv[1] from my main.c function, it could be something like this:
char address[200];
strcpy(address, argv[1]);
FILE *log;
log = fopen(address, "w");
I need the address pointed by *log to be visible in all my .c source files, because they will have some lines like this:
fprintf(log, "Comment to be printed on log.txt");
I know it's a bad way to make a log file, but my program is big enough so that changing all the log printing lines will take a considerable amount of time.
The file's address has to be defined using argv[1]. How do I define it before my functions can use it (And how do I make them identify it)?
Define log as a global variable, and declare it as external symbol by extern FILE *log; in other places that you use it.
Related
people of StackOverflow,
I'm pretty new to c and wanted to set myself a challenge:
Adding a binary file (like a .exe file) to another, so when the second one is started, bot files are executed. This is the code I tried:
FILE* fp1;
FILE* fp2;
fp1 = fopen("path_to_file1","rb");
fp2 = fopen("path_to_file2","ab");
fseek(fp1,0, SEEK_END);
int size = ftell(fp1);
rewind(fp1);
unsigned long buffer[size];
fread(buffer,sizeof(buffer),1,fp1);
fclose(fp1);
fwrite(buffer,sizeof(buffer),1,fp2);
fclose(fp2);
All of this is running in the main function.
The problem is when executing the code, instead of appending the first file to the second, it overrides the first one, and on execute only executes the second file.
I really hope you can help me :)
Your code is incorrect because by doing this
unsigned long buffer[size];
you're writing sizeof(buffer) which is sizeof(unsigned long) times too big. So the first file is appended to the second file all right, but a lot of garbage is appended too.
You should declare:
unsigned char buffer[size];
so sizeof(buffer) yields the correct result.
Also, check the return value of your fopen statements.
EDIT: it just hit me that you want to append 2 executables together. The above (fixed) code works to append 2 binary files (data files) together, but for executables it just doesn't work.
Appending an executable in the end of another one is likely to be ignored by the operating system. The first executable header contains the logical size of the program segments. It doesn't read the file beyond that (well, it can be done by a lot of hacking and it's called a virus), which explains that your updated files runs like if nothing was appended.
One could imagine running a disassembler on both files, modify start points+add a wrapper to call both start points, and reassemble to another executable.
The easiest way to execute one program, then the other one, just call them in a script (bash, .bat, whatever) or in C system calls.
system("path_to_file1");
system("path_to_file2");
I have a function that takes in a file pointer (FILE * file) and copies everything in that file. Now, is it possible to erase everything inside this file?
I do not have the file name so I can not use fopen(filename, "w") again.
I have stdio.h and string.h included.
You can use the ftruncate() system call to empty a file using its file descriptor, e.g.
ftruncate(fileno(fh), 0);
You will probably want to follow that up with a call to rewind(fh) so that any further writes to the file are made at the beginning, rather than at the previous offset.
I have one function which reads file and does the conversion part.
fp=fopen("newfile.txt","r");
Here i have copied this newfile.txt in project file and compiling in VC++ 2008 IDE.It works fine
I would like to read the file from a local drive directory path.is it possible to read the files from local drive.how to mention the path.If so please mention any example.
one more thing If i want to read all the files in that particular folder with out changing the name of text files in the above code. Suggest me any thing to do.
I dont want to change the file name manully in the code
You could use an absolute path to your file:
FILE* fp = fopen("c:\\your_dir\\your_file.txt", "r");
if(fp) {
// do something
fclose(fp);
}
or a relative path, assuming your file is located in c:/etc and your executable is located in c:/etc/executables:
FILE* fp = fopen("..\\your_file.txt", "r");
if(fp) {
// do something
fclose(fp);
}
I think you can use first program argument. It is a string containing path of executable. You can access it by usingint main(int argc, char *args[]) instead of int main(). The args[0] contains what you need. Just take a substring of it, to get the path and concatenate it with your filename.
Is it possible to retrieve a file's name in C? If a file is created like the following:
FILE *f = fopen ("foo.txt", "r");
And the "foo.txt" comes from a variable out of scope. Is there a way to retrieve it from the FILE struct?
You can't in fact retrieve the file names from your FILE objects.
However, if the name being passed to fopen is important to you, and that variable is out of scope for whatever reason, you could always wrap a FILE* in a struct, ie.
struct file_with_name {
FILE *f;
char *name; /* OR char name[MAX] */
}
At least that's what I'd do but it depends on what you're actually trying to do.
Unfortunately, on many systems a filename is just a pointer to an inode. And if those filesystems support hard links, that means that there are multiple filenames per real file. Also, you can open a file and then remove the file in which case there wouldn't even be a filename, just a descriptor. For these reasons, you can't go from a file descriptor to a file name.
Its not possible to get the filename out of a FILE handle. You should store the filename yourself if you later need to use it.
Since FILE objects may be created from things that have no name associated with them (in particular, from a raw file descriptor with fdopen()), they do not always have a name to get.
In general, it's not possible. The closest you can try, on Linux, is:
char buf[PATH_MAX];
snprintf(buf, sizeof buf, "/proc/self/fd/%d", fileno(f));
readlink(buf, buf, sizeof buf);
Reusing buf like that is horribly ugly, but as far as I can tell, it's legal and not undefined behavior per POSIX.
Assume I have a file pointer FILE* myfile. Is there a way to retrieve the name of the file where myfile is reading from or writing to?
Not in any CRT implementation that I've ever seen. It is useless info, you already have to supply the file name to get a FILE*. You could probably dig an operating system handle out of the FILE structure although you might need to hop through a file descriptor table. Your next problem is then the operating system support you'd need to map a file handle back to a file name. That should be difficult too.
I found a nice example that uses an overwritten struct MyFile:
How to get filename from a FILE*