Why doesn't fprintf(outputFile,"") work from inside gdb? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When I try to debug a piece of code from inside gdb and the control reaches a fprintf statement to an outputFile it doesn't show the output lively in the file and it just shows empty, despite working fine with just a simple run of the program from the terminal.
How do I get my output lively on the outputFile when debugging from inside gdb?
NOTE: I am not talking about gdb precisely I am just asking how to get the output lively in the file on the fly while debugging from gdb.

How do I get my output lively on the outputFile when debugging from inside gdb?
Do this:
(gdb) call fflush(0)
This will cause the program being debugged to flush all of its output streams.

This is probably due to stream buffering:
Characters that are written to a stream are normally accumulated and transmitted asynchronously to the file in a block, instead of appearing as soon as they are output by the application program. Similarly, streams often retrieve input from the host environment in blocks rather than on a character-by-character basis. This is called buffering.
Try out this example:
#include <stdio.h>
int main(void) {
FILE *f = fopen("foo.txt", "w");
printf("Break here and go line by line.");
fprintf(f, "Bar\n");
printf("Nothing in foo.txt yet.");
fflush(f);
printf("Buffer has been written to foo.txt now.");
fprintf(f, "Baz\n");
printf("Baz is still only in the buffer.");
fclose(f);
printf("The buffer was written to the file before closing.");
}

Related

fopen ordering in C affecting it strangely [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have this code:
FILE *setup=fopen(strcat(cwd,"setup.txt"),"r");//navigates up to setup file
...//(doing stuff with setup)
fclose(setup);
FILE *paths=fopen(strcat(cwd,"stuff.txt"),"r");
char buff2[1024];
fgets(buff2,200,paths);
char thing[1024];
strcpy(thing,buff2);
printf(thing);
fclose(paths);
but it executes differently based on if I do code with setup or the code with paths first - only the first one works properly, the second just creates gibberish whenever fgets is called. fclose returns 0 as it should. The specific way I look at the second file doesn't seem to affect it. What's happening here?
Don't strcat()!
FILE *setup=fopen(strcat(cwd,"setup.txt"),"r");//navigates up to setup file
// cwd now has "...setup.txt"
FILE *paths=fopen(strcat(cwd,"stuff.txt"),"r");
// cwd now has "...setup.txtstuff.txt"
strcat() modifies cwd.
So if cwd originally contains "/home/username/", the first call changes it to "/home/username/setup.txt".
The second call appends to that, so it tries to open "/home/username/setup.txtstuff.txt". This filename almost certainly doesn't exist, but you aren't checking for an error.
And if that filename is longer than the space allocated to cwd, you'll get a buffer overflow and undefined behavior.
Use a different variable to hold the filename to open.
char filename[MAXLENGTH];
sprintf(filename, "%s%s", cwd, "setup.txt");
FILE *setup = fopen(filename, "r");
...
fclose(setup);
sprintf(filename, "%s%s", cwd, "stuff.txt");
FILE *paths = fopen(filename, "r");
...
fclose(paths);

How can I print file path in C inside Minix OS? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to edit the exec syscall inside the Minix OS, in a way that every process created by it should print the program file path + program name:
for example, if I type ls inside the terminal, the next line should show me path/to/ls/ls
always in this format: filepath/programName.
I already managed to print the program name, but I am having problems with the file path. Can somebody show me how can I print it?
here is the code for sys_exec.c:
#include "syslib.h"
#include "stdio.h" //I included this library
int sys_exec(endpoint_t proc_ep, vir_bytes stack_ptr, vir_bytes progname,
vir_bytes pc, vir_bytes ps_str)
{
/* A process has exec'd. Tell the kernel. */
message m;
m.m_lsys_krn_sys_exec.endpt = proc_ep;
m.m_lsys_krn_sys_exec.stack = stack_ptr;
m.m_lsys_krn_sys_exec.name = progname;
m.m_lsys_krn_sys_exec.ip = pc;
m.m_lsys_krn_sys_exec.ps_str = ps_str;
//edited by me
puts((char*)progname);//this prints the program name
return _kernel_call(SYS_EXEC, &m);
}
and some image as reference:
this is what happens when I type ls
I was on the wrong path, the file I needed to edit was \usr\src\minix\minix\servers\vfs\exec.c, I was trying to edit \usr\src\minix\minix\lib\libsys\sys_exec.c.
Inside the right file I had access to a variable called fullpath inside the get_read_vp function. I imported stdio.h and then typed printf("executing: %s\n", fullpath); inside the function.

Getting a core segmentation fault when trying to read a file from argv[] [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have been getting a core segmentation fault when trying to read a file from argv[]. I have created the file and I am very sure that I am trying to access the correct memory address. However, when trying to open the file I receive the core dump.
Here is the relavent code-
for (int x=1;x<argc;x++){
int buffsize=2056;
char buff[buffsize];
FILE* thefile;
thefile=fopen("argv[x]","r");
if (thefile == NULL) {
fprintf(stderr, "%s cannot open file '%s'\n", argv[0], argv[1]);
return 2;
}
The command line argument I am passing in is
./words testfile.txt
with ./words being the compiled code.
I actually found the solution, it seems to be the quotes in argv in the fopen, why is this?
Oops! I believe the line
thefile=fopen("argv[x]","r");
Should be:
thefile=fopen(argv[x],"r");
This is because "argv[x]" is not code, it's a string. What's that? Is that a path to a file? It's not compiled as code. It doesn't refer to one of the arguments in argv.
You might also want your log message to use that filename. Notice the argv[x]:
fprintf(stderr, "%s cannot open file '%s'\n", argv[0], argv[x]);

how to read .docx file in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I got abstract output which you can not read by running this
code.please tell me,how I can read the output or different method by
which I can read word file in c.I am a beginner and didn't found any detail information about this.
#include <stdio.h>
int main()
{
FILE *fr;
int c;
fr = fopen("Hello.docx", "r");
if(fr==NULL)
{
printf(" File NOT FOUND!");
}
while( c != EOF)
{
c= fgetc(fr); /* read from file*/
printf("%c",c); /* display on screen*/
}
fclose(fr);
return 0;
}
Reading a MS Word document with raw C programming is quite a big project, not suitable for beginners. It is not a pure text file so you can't use fopen("Hello.docx", "r");. Rather it is a custom format, so you'd have to open it as binary. Then read the 500+ pages long specification of the format and treat the data accordingly. Might be worth taking a peek at the Open Office code to get an idea of how much work that's involved.
You can't directly open a word file using fopen( ) as it is difficult to convert a .docx file. Reason being, it is a huge binary file and is compressed, thus while converting it might be possible you don't get the entire data.
Take a look at fseek( ) that might help.

file opening and writing in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
FILE *fp1;
if((fp1 = fopen("abc.txt", "w")) != NULL)
{
fprintf(something);
fclose(fp1);
}
I am trying to compile this file and all related file using developer command prompt and trying to run it from there. But while running it says this filename.exe has stopped working. It is not able to create the file abc.txt .
You almost certainly don't have write permission in the directory you are attempting to create abc.txt in. The code would be better if you handled the error case:
if ((fp1 = fopen("abc.txt", "w")) == NULL) {
perror("abc.txt");
exit(1);
}
My guess is your output will be "abc.txt: Permission denied"
You also have a problem that your fprintf() ought to be fprintf(fp1, something) (where something is presumably your format string and some variables).

Resources