从文本文件中的字符串中的两个单词

问题描述 投票:3回答:5

我试图在一个字符串中得到两个单词而我不知道如何做到这一点。我试过但是如果在文本文件中我有'名字Penny Marie'它给了我:名字Penny。我怎样才能在s1中获得Penny Marie?谢谢

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

int main()
{
    printf("Hello world!\n");
    char s[50];
    char s1[20];

    FILE* fp = fopen("file.txt", "rt");
    if (fp == NULL)
        return 0;

    fscanf(fp,"%s %s",s,s1);
    {
        printf("%s\n",s);
        printf("%s",s1);
    }
    fclose(fp);
    return 0;
}
c
5个回答
3
投票

更改fscanf格式,告诉它​​在新行之前不要停止阅读:

fscanf(fp,"%s %[^\n]s",s,s1);


1
投票

你应该使用fgets

或者您可以尝试这样做:

fscanf(fp,"%s %s %s", s0, s, s1);
{
    printf("%s\n",s);
    printf("%s",s1);
}

并声明s0为空*


1
投票

其他答案针对您的fscanf呼叫进行调整,以满足您的需求。 (虽然fscanf()通常不是你要问的最好的方法。)你的问题是关于从包含Penny的文件中的一行中获取2个单词Mariename Penny Marie的具体信息。如评论中所述,如果文件包含多于一行需要解析,或者名称字符串包含可变数量的名称,该怎么办?通常,以下函数和技术更合适,更常用于从文件中读取内容并将其内容解析为字符串:

fopen() and its arguments. fgets() strtok()(或strtok_r()How to determine count of lines in a file(用于创建字符串数组) How to read lines of file into array of strings

部署这些技术和功能可以通过多种方式进行调整,以解析文件中的内容。为了说明,下面实现了一个使用这些技术的小例子,它将处理您所述的需求,包括每个文件多行和每行中可变数量的名称。

给定文件:本地目录中的names.txt

name Penny Marie
name Jerry Smith
name Anthony James
name William Begoin
name Billy Jay Smith
name Jill Garner
name Cyndi Elm
name Bill Jones
name Ella Fitz Bella Jay
name Jerry

下面按行数读取文件以表征其内容,最长行创建一个字符串数组,然后使用文件中的名称填充数组中的每个字符串,而不管名称的部分数量。

int main(void)
{
    // get count of lines in file:
    int longest=0, i;
    int count = count_of_lines(".\\names.txt", &longest);

    // create array of strings with information from above
    char names[count][longest+2]; // +2 - newline and NULL
    char temp[longest+2];
    char *tok;

    FILE *fp = fopen(".\\names.txt", "r");
    if(fp)
    {
        for(i=0;i<count;i++)
        {
            if(fgets(temp, longest+2, fp))// read next line
            {
                tok = strtok(temp, " \n"); // throw away "name" and space 
                if(tok)
                {
                    tok = strtok(NULL, " \n");//capture first name of line.
                    if(tok)
                    {
                        strcpy(names[i], tok); // write first name element to string.
                        tok = strtok(NULL, " \n");
                        while(tok) // continue until all name elements in line are read
                        {   //concatenate remaining name elements
                            strcat(names[i], " ");// add space between name elements
                            strcat(names[i], tok);// next name element
                            tok = strtok(NULL, " \n");
                        }
                    }
                }
            }

        }
    }
    return 0;
}


// returns count, and passes back longest
int count_of_lines(char *filename, int *longest)
{
    int count = 0;
    int len=0, lenKeep=0;
    int c;
    FILE *fp = fopen(filename, "r");
    if(fp)
    {
        c = getc(fp);
        while(c != EOF)
        {
            if(c != '\n') 
            {
                len++;  
            }
            else
            {
                lenKeep = (len < lenKeep) ? lenKeep : len;
                len = 0;
                count++;
            }
            c = getc(fp);
        }
        fclose(fp);
        *longest = lenKeep;
    }
    return count;       
}

0
投票

将你的fscanf线改为fscanf(fp, "%s %s %s", s, s1, s2)

然后你可以printf你的s1s2变量得到“便士”和“玛丽”。


0
投票

试试功能fgets

   fp = fopen("file.txt" , "r");
   if(fp == NULL) {
      perror("Error opening file");
      return(-1);
   }
   if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */
      puts(str);
   }
   fclose(fp);

在上面的代码中,它将写出最多60个字符的内容。如果我没有弄错的话,你可以用str(len)使那个部分变得动态。

© www.soinside.com 2019 - 2024. All rights reserved.