简体   繁体   English

从文件中读取并存储在C中

[英]Reading from a file and storing in C

I am trying to read in a text file of a string followed by a number and then store it's contents. 我试图读取一个字符串后跟一个数字的文本文件,然后存储它的内容。 So far I can only get it to print it out just the string (or just the int, or both) if it is properly formatted. 到目前为止,我只能将它打印出来只是字符串(或只是int,或两者),如果格式正确的话。 How can I skip blank or misformatted lines lines (which currently duplicated the previous line) and also store the results? 如何跳过空白或格式错误的线条(当前与前一行重复)并存储结果?

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

#define MAX_LINE_LENGTH 400

int main ()
{
    char input[MAX_LINE_LENGTH];
    char name[MAX_LINE_LENGTH];
    int number;

    FILE *fr;
    fr = fopen ("updates.txt", "r");
    if (!fr)
    return 1;  
    while (fgets(input,MAX_LINE_LENGTH, fr)!=NULL)
    {
        /* get a line, up to 200 chars from fr.  done if NULL */
        sscanf (input, "%s", name);
        /* convert the string to just a string */
        printf ("%s\n", name);
    }
    fclose(fr);
    return 0;
}

Example text file 示例文本文件

Cold 5
10 Flames

Doggy                      4

Flames 11
Cold 6

You may use fscanf function. 您可以使用fscanf函数。 A blank space in format string makes it to ignore any spaces, tabs or newlines. 格式字符串中的空格使其忽略任何空格,制表符或换行符。

Probable solution for your problem is in the code below. 您的问题的可能解决方案在下面的代码中。

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

#define MAX_LINE_LENGTH 400

int main ()
{
    char input[MAX_LINE_LENGTH];
    char name[MAX_LINE_LENGTH];
    char namet[MAX_LINE_LENGTH];
    int number;

    FILE *fr;
    fr = fopen ("updates.txt", "r");
    if (!fr)
    return 1;
    while (fgets(input,MAX_LINE_LENGTH, fr)!=NULL)
    {
        memset(name, 0, MAX_LINE_LENGTH);
        memset(namet, 0, MAX_LINE_LENGTH);
        /* get a line, up to 200 chars from fr.  done if NULL */
        //sscanf (input, "%s %d", name, &number);
        sscanf (input, "%s %s", name, namet);

        // TODO: compare here for name shall only contain letters A-Z/a-z
        // TODO: compare here for namet shall only contain digits

        // If both above condition true then go ahead
        number = atoi(namet);

        if(name[0] != '\0')
        {
                /* convert the string to just a string */
                printf ("%s %d\n", name, number);
                //printf ("%s %s\n", name, namet);
        }
    }
    fclose(fr);
    return 0;
}

Instead of 代替

while (fgets(input,MAX_LINE_LENGTH, fr)!=NULL)
{
  /* get a line, up to 200 chars from fr.  done if NULL */
  sscanf (input, "%s", name);
  /* convert the string to just a string */
  printf ("%s\n", name);
}

do this (it will remove all spaces and \\n and just take out the tokens) 这样做(它将删除所有空格和\\ n,只需取出令牌)

while (fgets(input,MAX_LINE_LENGTH, fr)!=NULL)
{
  char* token = strtok(input, " \n");
  while ( token != NULL )
  {
    printf( "%s", token );
    token = strtok(NULL, " \n");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM