以下代码应从用户获取字符串(包含存储中的项目数据)。
我在使程序迭代到新行时遇到问题。当我按下“输入”时它会中止,但我需要它只将下一行作为输入。代码:
for (i; i < STORE_SIZE; i++) {
fgets(seq, MAX_STRING_LEN, stdin);
if (strcmp(seq, "stop")) {
break;
}
else {
init(seq, ptr);
*(storage + i) = ptr;
}
}
Run Code Online (Sandbox Code Playgroud)
当程序在下一行中得到字符串“stop”时,程序应该中止,而不是按下回车键。
我想问一下我怎么能接受\r\n而不把它改成\\r\\n, with fgets.
我希望程序将 转换\r\n为换行符,而不是将其打印为字符串。
当前代码:
char buff[1024];
printf("Msg for server: ");
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff), stdin);
printf(buff);
Run Code Online (Sandbox Code Playgroud)
输入:
test\r\ntest2
Run Code Online (Sandbox Code Playgroud)
我想要的输出:
test
test2
Run Code Online (Sandbox Code Playgroud)
我目前的输出:
test\r\ntest2
Run Code Online (Sandbox Code Playgroud) 我做了一个程序,发现了gets(),并且使用它,它很棒,但是当我编译程序时它说得到危险,所以然后我搜索了一个替代方案,即使gets()函数做了它有正确的输出,为了将来我宁愿使用一个不危险的替代品,这是我偶然发现fgets().我认为代码会编译相同并具有相同的输出,但是我的if语句都没有输出.
下面是代码:
#include <stdio.h>
#include <string.h>
int main()
{
char qOne[10];
char qTwo[10];
char guess[40] = "My guess is that you are thinking of a\0";
printf("TWO QUESTIONS\n");
printf("Think of an object, and i'll try to guess it.\n\n");
printf("Question 1) Is it an animal, vegetable, or mineral?\n");
printf("\n> ");
//gets(qOne);
fgets(qOne, 10, stdin);
printf("\nQuestion 2) Is it bigger than a breadbox? (yes/no)\n");
printf("\n> ");
//gets(qTwo);
fgets(qTwo, 10, stdin);
printf("\n");
if(strcmp(qOne, "animal") == 0 && strcmp(qTwo, "no") == 0 ||strcmp(qOne, "animal") == …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
void main () {
char str[5];
fgets(str, sizeof(str), stdin);
printf("%s.", str);
}
Run Code Online (Sandbox Code Playgroud)
我用C编写了这个简单的代码,我试图在一行中打印一个字符串和一个点,但每当我输入一个包含3个或更少字符的字符串时,输出在字符串后面都有一个换行符.
输入:
abc
Run Code Online (Sandbox Code Playgroud)
输出:
abc
.
Run Code Online (Sandbox Code Playgroud)
如果我输入正好4个字符的内容,输出就是我想要的,没有换行符.
我已经尝试使用gets()和scanf()函数,但它们运行良好,但我无法使用它们.
有人知道它为什么会发生和解决方案吗?
这是fgets()从手册页的描述:
char *fgets(char *s, int size, FILE *stream);
...
RETURN VALUE
fgets() returns s on success, and NULL on error or when end of file
occurs while no characters have been read.
Run Code Online (Sandbox Code Playgroud)
它不遵循模式read,在失败时返回-1,在成功时返回读取的字节数.相反,它返回char*它NULL的失败和s成功.这没有给我任何关于输入多长时间的信息.所以,如果我有这样的事情:
char input_buffer[256];
fgets(input_buffer, sizeof(input_buffer), stdin);
Run Code Online (Sandbox Code Playgroud)
在fgets调用之后,有没有办法告诉输入有多长没有先初始化缓冲区?
谢谢.
我正在创建一个程序,您可以通过该程序执行终端命令。我想访问一个目录(使用 cd /Users/user/Desktop),但由于 scanf 在空格处终止,我被迫将终止值更改为[^\n]. 这时候错误就出现了。每当我输入命令时,它都会执行该命令,但是下次程序进入(无限)循环时,它不会停止执行 scanf 函数之前的行。当终止值为 时,这种情况不会发生%s。这是该程序的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void execute(){
char* command = (char *) malloc(15);
char* output = (char *) malloc(4096);
printf(">> ");
scanf("%[^\n]", command); //Here is the scanf function
FILE* cmd = popen(command, "r");
fread(output, sizeof(output), 32000, cmd);
if (strlen(output) != 0){
printf("\n%s\n", output);
}
free(output);
pclose(cmd);
}
int main(){
while (1){
execute();
}
}
Run Code Online (Sandbox Code Playgroud)
这是终止值为 时的输出[^\n]:
>> ls
Applications
Desktop
Documents
//Here the rest …Run Code Online (Sandbox Code Playgroud) fgets()是一个读取一行输入的安全函数,但它会将从文件中读取的新行字节存储'\n'在数组中(如果合适的话)。
在许多情况下(如果不是大多数情况),必须在进一步处理行内容之前删除此新行。
可以使用几种简单的方法,但我看到了一个紧凑而棘手的建议:
strtok(line, "\n"); // strip the newline
Run Code Online (Sandbox Code Playgroud)
为什么这种方法不正确,为什么它并不总是有效?
我正在尝试用C语言从用户输入中获取字符串,以便程序可以打开选定的文件。
我尝试使用fgets是因为我在许多线程上读到它是更安全的选择(而不是gets)。
但是,当使用gets存储字符串时,文件将打开,而使用fgets则不会。
这是我正在使用的代码:
char csvFile[256];
FILE *inpfile;
printf("Please enter CSV filename: ");
fgets(csvFile,256,stdin);
printf("\nFile is %s\n",csvFile);
inpfile = fopen(csvFile,"r");
if(inpfile == NULL)
{
printf("File cannot be opened!");
}
Run Code Online (Sandbox Code Playgroud)
我知道该文件存在,但是使用fgets输入了if块。
唯一的区别是使用:
gets(csvFile);
Run Code Online (Sandbox Code Playgroud)
代替
fgets(csvFile,256,stdin);
Run Code Online (Sandbox Code Playgroud)
谁能帮我理解这一点?提前致谢。
void verification() {
char pass[50];
printf(" Enter Password : ");
fgets(pass, 50, stdin);
if (pass != 'aaanc6400') { \\ Warning message in here
printf("\n Invalid Password.. Please enter the correct password. \n\n");
verification();
}
info();
}
Run Code Online (Sandbox Code Playgroud)
当我编译程序时,在标记的行上它显示警告"字符常量对于它的类型太长"以及"指针和整数之间的比较".然后当我运行代码并输入正确的密码时,它仍会输出密码错误.我究竟做错了什么?
我有个问题。使用 fgets 函数后,我尝试查看某些字符串的长度。如果我在字符串中可以包含的字母数下输入字符串(例如:字符串中的最大字母为 9,我输入 4 个字母),我会得到字符串的长度+1。为什么?
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char name[10]={0};
printf("enter your name\n");
fgets(name, 10, stdin);
printf("your name is %s and it is %d letters\n", name, strlen(name)); // length problem
return 0;
}
Run Code Online (Sandbox Code Playgroud)