snprintf does not create the correct command [closed] - c

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 6 years ago.
Improve this question
When I'm debugging my program with gdb and type print command, it prints this :
$4 = "sudo asterisk -rx \"pjsip show aor 101\"\000\000b\006#\000\000\000\000\000p\004#\00 ... and so on
Normaly it should just create the plain command as a char? If I am right. This leads to another problem, because popen() can not create a proper stream.
Here is my important code:

Since command is an array (not a char *) when you tell gdb to print command, it prints the entire contents, including anything in the buffer after the trailing NUL character. So when you see:
$4 = "sudo asterisk -rx \"pjsip show aor 101\"\000\000b\006#\000\000\000\000\000p\004#\00
^^^^ -- NUL character
Its showing you what snprintf put into the buffer, as well as whatever random stuff happened to be in memory on the stack when the function was started and stack space was reserved for command

Related

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

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.");
}

main.c:16:9: error: stray '\32' in program in C [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 am trying an example from the GNU C Programming Tutorial (page 93) that uses a few of the math library routines listed.
#include <stdio.h>
#include <math.h>
int main() {
double my_pi;
my_pi = 4 * atan(1.0);
/* Print the value of pi, to 32 digits */
printf("my_pi = %.32f\n", my_pi);
/* Print value of pi from math library, to 32 digits */
printf("M_PI = %.32f\n", M_PI);
return 0;
}
When I compile the file 'main.c' using MinGw using the command
gcc main.c -o main -lm
It gives the following error:
main.c:16:9: error: stray '\32' in program
16 : }
: ^
Error occurred because of using Turbo C to edit 'main.c' which adds → character at the end of the curly brackets. That is why compilation fails in MinGw..
The code \32 is an ASCII control character ^Z aka EOF - the End-Of-File (see https://en.wikipedia.org/wiki/End-of-file#EOF_character). It was appended at the end of the text file with some (DOS-like?) editing tool – or maybe you copied a source code and pasted it through some shell command to a file, which resulted in appending the EOF byte.
Try using some other editing tool to strip the last byte from your main.c file. Maybe adding a newline after the closing bracket would be good start.

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.

How to fix an error of reading a text file [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 3 years ago.
Improve this question
I have tried to reading a text file but the array stores only the last line of the text file.
char cleanedText[10000]={'\0'};
while(!feof(fileptr))
{
fgets(cleanedText, 10000 , fileptr);
}
But,the cleanedText stores only the last sentence which is written in the text line.
For example these two is written in the text,
asdasdasd
thisisus
When i print the cleanedText.
The screen just shows thisisus.
What is the problem?
Each call to fgets writes into the same place, overwriting what was there previously.

How do I run a C program from my Windows command prompt? [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 8 years ago.
Improve this question
Using Vim as an editor, I wrote the following simple code in C and saved it as helloworld.c :
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
}
In command prompt, I wrote:
start chrome helloworld.c
This caused my browser to open up the file, but it did not print Hello World. Instead, it just displayed the code I had written. Did I not save it as a C file?
Also, I was wondering how to display the result of my C program inline on command prompt, as I am fairly new to it. While searching the internet, I could not find any answers. Am I supposed to do so from Vim? I learned that you are supposed to do ./ in the gedit command box to display the result inline, but this does not work for the one that comes with Windows.
Please help and thank you for taking the time to read and answer.
As #Ernest Friedman-Hill has already said, you normally have to compile the program. However, there are alternatives.
One alternative is the Tiny C Compiler, from http://bellard.org/tcc/. TCC does allow you to run the program without compiling it.
tcc -run helloworld.c
Does exactly what you want.
The Tiny C Compiler is not the only way to run C code from source without compiling it first. There are a few other alternatives.
CSL: http://csl.sourceforge.net/csl.html
Ch: https://www.softintegration.com/
PicoC: https://code.google.com/p/picoc/
CINT: http://root.cern.ch/drupal/content/cint
I hope this helps.

Resources