相关疑难解决方法(0)

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
查看次数

为什么strstr似乎没有在我的C程序中工作?

源代码如下:

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

char tracks[][80] = {
"I left my heart in Harvard Med School",
"Newark, Newark - a wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};


void find_track(char search_for[]) {
    int i;
    for (i=0; i<5; i++) {
        if (strstr(tracks[i], search_for)) {
            printf("Track %i: '%s'\n", i, tracks[i]);
        }
    }
}

int main() {
    char search_for[80];
    printf("Search for:");
    fgets(search_for, 80, stdin);
    find_track(search_for);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我使用该程序时如下:

搜索:城镇

我希望有类似的东西

第1轨:'纽瓦克,纽瓦克 - 一个美妙的小镇' …

c

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

如何从fgets()输出的末尾修剪'\n'

我想从fgets输出修剪换行符.

while(fgets(buff,1024,fp) ){

    printf("start%send",buff );
Run Code Online (Sandbox Code Playgroud)

如果文件中有一行"C很酷".然后打印上面的代码.

startC is cool
end 
Run Code Online (Sandbox Code Playgroud)

但我希望它打印出来

 startC is coolend 
Run Code Online (Sandbox Code Playgroud)

怎么做

c

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

为什么我的 %[^\n] 不能正常工作?

int main() {
    char a[100];

    printf("\nEnter 1st Sentence : ");
    scanf("%[^\n]", a);
    printf("\nSentence 1 : %s", a);

    printf("\nEnter 2nd Sentence : ");
    scanf("%[^\n]", a);
    printf("\nSentence 2 : %s", a);

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

我的输出:

Enter 1st Sentence : Testing 1st Sentence
Sentence 1 : Testing 1st Sentence
Enter 2nd Sentence : 
Sentence 2 : Testing 1st Sentence
Run Code Online (Sandbox Code Playgroud)

我基本上是在检查%[^\n]。当我输入第一个句子并按“Enter”键时,它会打印“Enter 2nd Sentence :”,然后打印“Sentence 2 :”,然后打印第一个句子。

c

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

检查C中是否存在.txt文件

我试图创建一个从用户那里获取文件名并检查是否存在的函数,只要它不存在,我希望再次询问用户输入文件,直到找到确实存在的文件。最后,我希望将该文件的名称返回给main,以便在其他函数中使用。

我创建了一个我认为可以使用的函数,但是似乎我在调用变量而不直接输入文件名时遇到了问题。(我不认为这是问题所在)。

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

char* fileCheck();
char* fileCheck()
{
    char* fileName;
    printf("Please enter a file name: ");
    gets(fileName);
    while (access(fileName, F_OK) == -1)
    {
        printf("The File %s was not Found\nPlease enter a new file name: ", fileName);
        gets(fileName);
    }
    return fileName;
}

int main (void)
{
    fileCheck();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望函数的输出是有效的(现有)文件名。实际上,我收到的是垃圾号码退出代码。

c function

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

C字符串字符串比较总是导致false

我正在尝试在文件中查找字符串。我通过修改的手册页中存在的代码段编写了以下内容getline

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

int main(void)
{
    FILE * fp;
    char * line = NULL;
    char *fixed_str = "testline4";
    size_t len = 0;
    ssize_t read;

    fp = fopen("test.txt", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);

    while ((read = getline(&line, &len, fp)) != -1) {
        printf("Retrieved line of length %zu:\n", read);
        printf("%s", line);

        if (strcmp(fixed_str,line)==0)
            printf("the match is found\n");
    }
    //printf("the len of string is %zu\n", strlen(fixed_str));

    fclose(fp);
    if (line)
        free(line);
    exit(EXIT_SUCCESS);
} 
Run Code Online (Sandbox Code Playgroud)

问题是,尽管getline成功且正确地遍历了文件中的所有行,但strcmp的结果始终为false。由于换行符(AM I …

c linux string file

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

如何在不使用字符串函数的情况下删除 fgets 函数的 \n

有没有办法从使用 fgets 读取的字符串末尾删除 \N 字符,而不使用字符串处理函数(strlen、strtok、strcspn 等)?我试过了,但没用

char* remove_newline_char(char *str){
    for(int i = 0; i < str[i]; i++){
        if(str[i] == '\n'){
            str[i] = '\0';
        }
    }
    return str;
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
2
解决办法
121
查看次数

如何在C程序中的“ ctime”之后删除换行符?

因为我还是C编程的新手,所以请多多包涵。当我运行此代码时:

#include <time.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    while (1) {
        time_t mytime;
        mytime = time(NULL);
        printf("%s Hello world\n", ctime(&mytime));
        sleep(1);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出总是看起来像这样:

Wed Jan 18 02:32:32 2017
 Hello world
Wed Jan 18 02:32:33 2017
 Hello world
Wed Jan 18 02:32:34 2017
 Hello world
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

Wed Jan 18 02:32:32 2017 Hello world
Wed Jan 18 02:32:33 2017 Hello world
Wed Jan 18 02:32:34 2017 Hello world
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

注意:如果我将其删除\nprintf("%s Hello world\n", ctime(&mytime)); …

c

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

标签 统计

c ×9

string ×2

arrays ×1

char ×1

fgets ×1

file ×1

file-io ×1

function ×1

linux ×1