小编Ana*_*ubi的帖子

同时从文件中写入和读取

我一直试图同时从文件中读取和写入,并且我试图将text.txt内容中的所有选项卡替换为空格.这是我的代码:

int main()
{
   FILE* filePtr = fopen("text.txt", "w+");

   char c;
   c = fgetc(filePtr);
   fpos_t num;
   while(c != EOF)
   {
       if(c == '\t')
       {
           fgetpos(filePtr, &num);
           num--;
           fsetpos(filePtr, &num);
           fputc(' ', filePtr);
       }
       c = fgetc(filePtr);
   }
}
Run Code Online (Sandbox Code Playgroud)

text.txt的内容如下:

嗨\ t我的名字\ t是\ t杰克!

当我运行此代码时,text.txt文件中的输出只是空格.那里没有人物.我该怎么做才能让替换按预期进行?

c file writefile readfile

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

C中的字符串搜索

在下面的代码中,strstr()函数似乎没有正确响应 - 不运行if()语句,因为strstr()始终给出空指针,尽管输入字符串(变量search_for)存在于数组中.经过几个小时的挫折,我现在放弃了.请帮忙!

#include <stdio.h>
#include <string.h>
char tracks[][80] = {
    "impossible",
    "how to grow strong",
    "love is in the air",
    "3 prophets of everland",
    "home"
};
void find_track(char search_for[])
{
    int i;
    for (i = 0; i < 5; i++)
    {
        if (strstr(tracks[i], search_for))
            printf("Track %i: %s", i, tracks[i]);
    }
}
int main(void)
{
    char search_for[80];
    printf("Search for: \n");
    fgets(search_for, 80, stdin);
    printf("%s", search_for);
    find_track(search_for);

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

c arrays

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

标签 统计

c ×2

arrays ×1

file ×1

readfile ×1

writefile ×1