How to accept input with ints and strings with spaces? [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to accept input from a file. This file is in the format of an integer, followed by a tab, followed by a string (which may or may not have spaces).
for example:
1\tls -l
2\tls
I tried using:
int cmd_num;
char command[80];
while (fscanf(ifp, "%d\t%s", &cmd_num, command) != EOF) {
...
}
However, this failed when it saw a space. Any help?

You probably need to use a scan-set to read the string:
if (fscanf(fp, "%d\t%79[^\n]\n", &cmd_num, command) != 2)
...error handling...
else
...use cmd_num and command...
Note the size constraint in the format string to prevent buffer overflow.
Note, too, that you will not know whether the newline is matched. You might be better off using fgets() to read the whole line (or getline()), and then using sscanf() instead of fscanf(); at least you'll know whether the newline was collected (and can gobble to the newline if necessary).

Try this in your fscanf function:
fscanf(fp,"%d\t%[^\n]s",&cmd_num,command);
This will surely work...

Related

After I use malloc, program suddenly asks for input [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm a newbie programmer
//allocating space for final output
//text_count is given by another function, let's just say i have a value for it
//out is my typedef struct
out *fin[text_count];
for(i=0; i<text_count; i++){
fin[i] = malloc(sizeof(out));
}
//this is a test
fin[1]->appearances = 1;
printf("%d",fin[1]->appearances);
// ..other code
I noticed that after this allocation, program suddenly outputs this:
1_
The underscore is blinking, indicating it needs an input.
What can be my problem here? Is it the allocation? Or the codes down below?
okay sorry, let me clarify this, im using codeblocks . so when i get a blinking cursor it means it needs an input. and after i got this output, an infinite loop of inputs seems to be happening.
You're simply seeing the text terminal's cursor. It may be blinking or it may not, that doesn't mean anything in itself.
You might want to add a linefeed:
printf("%d\n", fi[1]->appearances);
to get the output on a line of its own.
A cursor after your printf does not mean, that it is waiting for input.
You can confirm this, by using strace, if it is waiting at read syscall.
Usage:
strace ./myApplication arg1 arg2...

Why does char * malloc give me a seg fault with scanf? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
char * input = (char*)malloc(256);
scanf("%s", input);
Seg fault. Why? I have a lot of trouble with strings in C, but I feel like everything is done right here. I even followed an example from a 100k+ SO user to the letter.
The bug could be caused by:
malloc() failing to allocate any memory, check the result against NULL.
by a buffer overrun from scanf, more than 256 characters.
by forgetting to include <stdlib.h> and then typecasting the result of malloc. More info here.
You need to ensure your buffer is large enough, including enough space for the trailing NUL (0 byte) at the end of the string. Otherwise, you get a buffer overflow that may lead to a segfault.

error expected primary-expression before 'char' [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i have a question,
i am trying this
if (strncmp(m_DSServer, "TCP:", 4) != 0 )
return DS_AS_PROCESS_NAME_INCORRECT;
if
if(strchr(char *(m_DSServer[4]),':')== NULL) //here it is giving me primary-expression before 'char
return DS_AS_PROCESS_NAME_INCORRECT;
else
if(strchr(m_DSServer[4],'/')== NULL)
return DS_AS_PROCESS_NAME_INCORRECT;
If you want to start searching from the 5. character, do
strchr(&m_DSServer[4],':')
Firstly, cast syntax that has the type(value) form is a chiefly C++ syntax. It is not supported in C. And your question is tagged [C], not [C++]. In C language you have to use the (type) value syntax when you want to perform a cast.
Secondly, even in C++ the type(value) cast syntax requires the type part to consist of a "compact" type specifier, i.e. even in C++ you can't use char * in this context.
Thirdly, regardless of the syntax you use, it is entirely not clear what you are trying to do by casting m_DSServer[4] value (which is apparently a char) to pointer type. This just does not make any sense.
If you wanted to do a search for a : character starting from the 4th position in string m_DSServer, you should do something like strchr(&m_DSServer[4], ':'). No casts necessary.
if (strchr(char *(m_DSServer[4]),':') == NULL)
^^^^^^^^^^^^^^^^^^^^^
char *(m_DSServer[4]) is nonsense. My guess is that you want to search the string for :, starting from the 4th character. In that case, you want a pointer to the 4th character:
if (strchr(m_DSServer+4,':') == NULL)
^^^^^^^^^^^^

Facebook interview: Implement readline function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
The question is
Implement a function char* readLine(); which returns single lines from
a buffer. To read the buffer, you can makes use of a function int
read(char* buf, int len) which fills buf with upto len chars and
returns the actual number of chars filled in. Function readLine can be
called as many times as desired. If there is no valid data or newline
terminated string available, it must block. In order to block, it can
use read function which in turn will block when it doesn't have
anything to fill the buf.
I don't know how to proceed and what kind of approach is expected.
I could not understand what the interviewer expects here. I just need direction to move ahead and not the exact function.
Allocate an appropriately sized buffers.
If you don't have characters in your read buffer, read in a new chunk.
If the next character from the read buffer is a newline, return the result buffer.
If the result buffer is full, bug out and whine about lines being too long.
Otherwise, add the next character from the read buffer into the result buffer.
NOTE: The answer to the question as asked is a security issue waiting to happen, and also a potential memory leak.

Linux system() API [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Can anybody tell me the output of the below code, whether "bye" will be printed or not?
#include <stdio.h>
int main()
{
system("ls -l");
printf("bye");
return 0;
}
man system says:
int system(const char *command);
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.
And after system() returned, your printf will be executed.
Yes, it will be printed.
Why don't you compile it and test it for yourself? Cut and paste what you've written to a file, e.g. foo.c, and then do the following
gcc -o foo foo.c
./foo
As there is no newline character after your printf("bye") it will end up at the start of your current line; putting printf("bye\n") instead will be a little more clear.
Why wouldn't it? There's no conditional statement, so every part of the code will be executed.
It will print the output of the command provided by system and then print "bye"
The program is going to execute ls -l just fine.
Also, "bye" should indeed be printed. However, since you have not included a '\n' character you will only be seeing it with your prompt appended to it.
Also, if you are not seeing it, then for some reason your output is not flushing when the program exits. Adding a '\n' character may very well fix that issue if that is what you are seeing.
It all depends upon the free memory.
You will need to check return value of the system command.it returns 0 on success.
Your printf statement will be called but not sure that system command will always success.
Thanks,
Neel

Resources