相关疑难解决方法(0)

c 中 printf 出现意外换行

我正在学习c语言中的字符串,并且正在做我的作业,要求我编写一个程序来在某些情况下替换部分字符串。这是我的源代码(未完成):

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[128], str2[128], str3[128];
    for (int i = 0; i < 128; i++) //initialize str
    {
        str1[i] = 0;
        str2[i] = 0;
        str3[i] = 0;
    }
    printf("Input the first string:"); //inputs
    fgets(str1, 128, stdin);
    printf("Input the second string:");
    fgets(str2, 128, stdin);
    printf("Input the third string:");
    fgets(str3, 128, stdin);
    if (strncmp(str1, str2, strlen(str2) - 1) == 0) //if the first n charters match (n=length of str2)
    {
        printf("%s", str3); //print str3
        int RemainingChar …
Run Code Online (Sandbox Code Playgroud)

c string printf

2
推荐指数
1
解决办法
972
查看次数

c从文件读取输入时修剪换行符

从文件中读取换行符后,这是一种安全的方法来修剪换行符吗?

while ( fgets(buffer, 1024, fp) != NULL )
{
    buffer[strlen(buffer)-1] = '\0';
    fprintf (stderr, "%s\n", buffer);
}
Run Code Online (Sandbox Code Playgroud)

它没有给我任何seg故障,但它可能会导致问题吗?我应该这样做吗?

while ( fgets(buffer, 1024, fp) != NULL )
{
    tmp = (char *) malloc(strlen(buffer));
    strncpy(tmp, buffer, strlen(buffer) - 1);
    tmp[strlen(buffer)-1] = '\0';
    fprintf (stderr, "%s\n", tmp);
}
Run Code Online (Sandbox Code Playgroud)

c file-io

1
推荐指数
1
解决办法
2万
查看次数

在用fgets()填充后,如何从我的字符串数组中删除换行符"\n"?

我正在使用的数组是:

char arr[91][12];
Run Code Online (Sandbox Code Playgroud)

所以我使用for循环从文件填充我的数组,如下所示:

for(i = 0; fgets(arr[i], 12, inFile); i++){}
Run Code Online (Sandbox Code Playgroud)

当我想从该数组中打印任何内容时,它会自动转义到下一行.

测试该数组:

for(i = 0; fgets(arr[i], 12, inFile); i++){
    printf("%s", arr[i]);
}

//one
//two
//three etc.

//want it to be 'one two three etc.'
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用strpbrk()来查找\n数组中的每个字符串并将其更改为\0:

for(i = 0; fgets(arr[i], 12, inFile); i++){
if(strpbrk(arr[i], "\n") != NULL ){
    arr[i][strpbrk(arr[i], "\n")] = '\0';
    }
printf("%s", arr[i]);
}
Run Code Online (Sandbox Code Playgroud)

但这给了我错误.有没有更好的方法来做我想做的事情?

c arrays string fgets char

1
推荐指数
1
解决办法
6219
查看次数

在C中输出开头打印一个额外的空行

为什么在输出开头打印一个空行?\n仅在%s之后...请帮助,我很沮丧.

if(argc > 1){

    while(r!=NULL){

        r = fgets(str, MAXL, stdin);

        if(r==NULL){
            return 0;
        }

        if (*argv[1] == 'i'){
            char *invP = inv(r);
            printf("%s\n", invP);
            free(invP);
        }
Run Code Online (Sandbox Code Playgroud)

inv()是:

char* inv(char* C){
int length = 0;

int i;
for(i = 0; C[i]!='\0'; i++){
    length++;
}

char *inverted;
inverted = malloc(length+1);
inverted[length] = '\0';
char* invP = inverted;

int j = 0;
for(i = length - 1; i >= 0; i--){
    inverted[j] = C[i];
    j++;
}
return invP;
}
Run Code Online (Sandbox Code Playgroud)

它没有任何印刷品,不知道为什么会发生这种情况.

c printing printf line output

1
推荐指数
1
解决办法
929
查看次数

为什么gets()假设extern在C中的代码中返回int?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int length;
    char **lines, buffer[80];

    printf("How many lines do you want to enter?.\n");
    scanf("%d", &length);
    getchar();
    lines = (char**)malloc(length * sizeof(char*));
    for (i = 0; i < length; ++i) { //Scan user's lines
        gets(buffer);
        lines[i] = (char*)malloc(strlen(buffer) * sizeof(char) + 1);
        if (!lines[i]) { //Check if allocation available
            printf("Error! Out of memory!");
            return endProgram;
        }
        strcpy(lines[i], buffer);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么gets()函数给我一个关于假设extern返回int的错误,我只是想扫描一个字符串.

c gets

1
推荐指数
1
解决办法
2678
查看次数

检查数组是否为回文的程序

我正在尝试创建一个程序,该程序检查给定的数组/字符串是否是回文,并且不起作用。该程序仅在每个给定的数组上打印“ 0”,即使在回文上也是如此。

int main()
{

    char string[100]= {0};
    char stringReverse[100]= {0};

    int temp = 0;
    int firstLetter = 0;
    int lastLetter = 0;

    printf("Please enter a word or a sentence: ");
    fgets(string, 100, stdin);

    strcpy(stringReverse , string); // This function copies the scanned array to a new array called "stringReverse"

    firstLetter = 0;
    lastLetter = strlen(string) - 1;    //because in array, the last cell is NULL

    // This while reverses the array and insert it to a new array called …
Run Code Online (Sandbox Code Playgroud)

c string palindrome

1
推荐指数
1
解决办法
1万
查看次数

从C++字符串中删除EOL但保留'\ 0'

这可能是一个愚蠢的问题,但如果我有一个C++字符串如下,

str = "the cat is black\n"
Run Code Online (Sandbox Code Playgroud)

如果我删除最后一个字符,即'\n',这是否会留下一个空终止字符串?下面的语句是否适用于从原始字符串str创建空终止的char*?

char *c = str.erase(str.size()-1).c_str();
Run Code Online (Sandbox Code Playgroud)

c++ string

1
推荐指数
1
解决办法
158
查看次数

C - Strcmp()不起作用

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{

const int SIZE = 100;

char input[SIZE];

while(1)
{
    fgets (input, SIZE - 2, stdin);          // input
    printf("%d", strcmp(input, "exit"));    //returining 10 instead of 0

    if(strcmp(input, "exit") == 0)
    {
        printf("SHELL Terminated\n");
        exit(0);    
    }

return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正面临一个问题.如果我在变量中输入exitinput,则函数strcmp()返回10,但它应该返回0并退出程序,因为exit等于exit.但事实并非如此.

我找不到问题.

c strcmp

1
推荐指数
3
解决办法
932
查看次数

从 fgets() 输入中删除换行符

我正在尝试编写一个非常简单的程序,它要求您输入您的姓名,然后它会向您致意。代码如下:

#include <stdio.h>

int main() {
    char name[30];
    printf("Write your name: ");
    fgets(name, 30, stdin);
    printf("Hello, %s. Nice to meet you.\n", name);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译并运行程序时,会显示以下输出(输入名称John Doe):

#include <stdio.h>

int main() {
    char name[30];
    printf("Write your name: ");
    fgets(name, 30, stdin);
    printf("Hello, %s. Nice to meet you.\n", name);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

起初我打算使用gets()which 显然不打印换行符,但编译器(GCC)抱怨它:

`gets' 函数是危险的,不应使用。

如何从输入的字符串中删除换行符fgets()

c printf gcc newline fgets

1
推荐指数
1
解决办法
7498
查看次数

C strptime 和 mktime 错误的时间戳

我使用以下方法从文本文件解析日期时间:

#define DATETIME_FORMAT "%Y-%m-%d %H:%M:%S"
Run Code Online (Sandbox Code Playgroud)
  if (!strptime(datetimestr, DATETIME_FORMAT, &record->datetime)) {
    return 0;
  } else {
    record->message_size = strlen(messagestr) - 1;
    messagestr[record->message_size] = '\0';
    strcpy(record->message, messagestr);
    printf("parsed '%s' '%s' %ld\n", messagestr, datetimestr, mktime(&record->datetime));
    return 1;
  }
Run Code Online (Sandbox Code Playgroud)

我得到了这些印刷品

parsed 'ETC' '2023-04-03 09:00:19' 1680508819
parsed 'StandUp' '2023-04-03 09:00:47' 1680508847
parsed 'Stop' '2023-04-03 09:11:55' 1680505915
Run Code Online (Sandbox Code Playgroud)

但是当我将它们粘贴到在线纪元转换器时,我得到:

  • 1680508819 => GMT:2023 年 4 月 3 日星期一 8:00:19
  • 1680508847 => GMT:2023 年 4 月 3 日星期一 8:00:47
  • 1680505915 => GMT:2023 年 4 月 3 日星期一 …

c datetime

1
推荐指数
1
解决办法
98
查看次数

标签 统计

c ×9

string ×4

printf ×3

fgets ×2

arrays ×1

c++ ×1

char ×1

datetime ×1

file-io ×1

gcc ×1

gets ×1

line ×1

newline ×1

output ×1

palindrome ×1

printing ×1

strcmp ×1