忽略c行中的第一个单词

忽略c行中的第一个单词,c,C,我正在编写一个代码,需要一些帮助 有一行需要从文件中读取。必须忽略第一个字,其余字符(包括空格)必须存储到变量中。我怎么做?一些例子。阅读程序中的注释以了解效果。这将假定单词由空格字符分隔(由isspace()定义)。根据您对“单词”的定义,解决方案可能会有所不同 #include <stdio.h> int main() { char rest[1000]; // Remove first word and consume all space (ASCII = 3

我正在编写一个代码,需要一些帮助


有一行需要从文件中读取。必须忽略第一个字,其余字符(包括空格)必须存储到变量中。我怎么做?

一些例子。阅读程序中的注释以了解效果。这将假定单词由空格字符分隔(由
isspace()
定义)。根据您对“单词”的定义,解决方案可能会有所不同

#include <stdio.h>

int main() {
    char rest[1000];
    // Remove first word and consume all space (ASCII = 32) characters
    // after the first word
    // This will work well even when the line contains only 1 word.
    // rest[] contains only characters from the same line as the first word.
    scanf("%*s%*[ ]");
    fgets(rest, sizeof(rest), stdin);
    printf("%s", rest);

    // Remove first word and remove all whitespace characters as
    // defined by isspace()
    // The content of rest will be read from next line if the current line
    // only has one word.
    scanf("%*s ");
    fgets(rest, sizeof(rest), stdin);
    printf("%s", rest);

    // Remove first word and leave spaces after the word intact.
    scanf("%*s");
    fgets(rest, sizeof(rest), stdin);
    printf("%s", rest);

    return 0;
}
#包括
int main(){
剩余碳[1000];
//删除第一个字并使用所有空格(ASCII=32)字符
//在第一个字之后
//即使该行仅包含1个单词,这也可以很好地工作。
//rest[]仅包含与第一个单词来自同一行的字符。
扫描频率(“%*s%*[]”);
fgets(剩余,剩余尺寸,标准尺寸);
printf(“%s”,其余);
//删除第一个单词并删除所有空白字符
//由isspace()定义
//如果当前行不可用,则从下一行读取rest的内容
//只有一个词。
scanf(“%*s”);
fgets(剩余,剩余尺寸,标准尺寸);
printf(“%s”,其余);
//删除第一个单词,并在单词后保留空格。
scanf(“%*s”);
fgets(剩余,剩余尺寸,标准尺寸);
printf(“%s”,其余);
返回0;
}

如果您的单词前面没有空格,并且您使用空格(“”)作为分隔字符,则此操作将有效

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

int main()
{
    char buffer[80];
    char storage[80];
    fgets(buffer, 80, stdin); // enter: »hello nice world!\n«
    char *rest = strchr(buffer, ' '); // rest becomes » nice world!\n«

    printf("rest:   »%s«\n", rest); // » nice world!\n«
    printf("buffer: »%s«\n", buffer); // »hello nice world!\n«

    strncpy( storage, rest, 80 ); // storage contains now » nice world!\n«
    printf("storage: »%s«\n", storage); // » nice world!\n«

    // if you'd like the separating character after the "word" to be any white space
    char *rest2 = buffer;
    rest2 += strcspn( buffer, " \t\r\n" ); // rest2 points now too to » nice world!\n«
    printf("rest2:  »%s«\n", rest2); // » nice world!\n«

    return 0;
}
#包括
#包括
int main()
{
字符缓冲区[80];
字符存储[80];
fgets(缓冲区,80,标准输入);//输入:»你好,美好的世界!\n«
char*rest=strchr(缓冲区“”);//rest变成了»美好的世界!\n«
printf(“rest:»%s«\n”,rest);/»美好世界!\n«
printf(“缓冲区:»%s«\n”,缓冲区);/»你好,美好的世界!\n«
strncpy(存储,rest,80);//存储现在包含»美好世界!\n«
printf(“存储:»%s«\n”,存储);/»美好世界!\n«
//如果您希望“单词”后面的分隔字符是任何空白
char*rest2=缓冲区;
rest2+=strcspn(缓冲区“\t\r\n”);//rest2现在也指向»美好世界!\n«
printf(“rest2:»%s«\n”,rest2);/»美好世界!\n«
返回0;
}

scanf(“%*s”)
然后后跟
fgets
,它将删除第一个单词后面的空格。如果要保留它,
scanf(“%*s”)
读取行并存储在变量中,然后删除第一个单词。nhahtdh,请您发布一个示例,我尝试了您所说的,但没有好结果。Dev,我使用fgets从行中读取,使用strcpy将其存储到变量中,但是,我如何删除第一个单词?@Jack Jackson,尽管这与您要求的内容无关。为了通知p前面的评论,提到他们的用户名前缀为
@
符号,这样
nhahtdh
就变成了
@nhahtdh
,然后只有他能听。@nhahtdh,你能不能发一个例子,我试过你说的,但没有好结果。