读取标签在C分离文件 [英] Read files separated by tab in c

查看:123
本文介绍了读取标签在C分离文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的C,和读取文件的事让我发疯...
我想读,包括文件名的文件,出生地点和电话号码等全部由制表符分隔

I am really new to C, and the reading files thing drives me crazy... I want read a file including name, born place and phone number, etc. All separated by tab

格式可能是这样的:

The format might be like this:

Bob Jason   Los Angeles    33333333
Alice Wong  Washington DC  111-333-222

所以我创建了一个结构来记录它。

So I create a struct to record it.

typedef struct Person{
    char name[20];
    char address[30];
    char phone[20];
} Person;

我试了很多方法来读取这个文件放到结构,但它失败了。
我累了FREAD:

I tried many ways to read this file into struct but it failed. I tired fread:

read_file = fopen("read.txt", "r");
Person temp;
fread(&temp, sizeof(Person), 100, read_file);
printf("%s %s %s \n", temp.name, temp.address, temp.phone);

但字符的字符串不计入临时由制表符分隔,它读取整个文件到temp.name并得到奇怪的输出。

But char string does not recorded into temp separated by tab, it read the whole file into temp.name and get weird output.

然后我尝试的fscanf和sscanf,这些都没有工作分离标签

Then I tried fscanf and sscanf, those all not working for separating tab

fscanf(read_file, "%s %s %s", temp.name, temp.address, temp.phone);

或者

fscanf(read_file, "%s\t%s\t%s", temp.name, temp.address, temp.phone);

这由分离空间字符串,所以我得到Bob和杰森分别,而事实上,我需要得到鲍勃·詹森作为一个字符的字符串。我没有当我创建的文本文件由制表符分隔这些格式。

This separates the string by space, so I get Bob and Jason separately, while indeed, I need to get "Bob Jason" as one char string. And I did separate these format by tab when I created the text file.

同为sscanf的,我尝试不同的方法很多次...

Same for sscanf, I tried different ways many times...

请帮忙...

推荐答案

我建议:


  1. 使用与fgets 来逐行读取文本行。

  2. 使用 strtok的通过使用标签作为分隔符的行的内容分开。

  1. Use fgets to read the text line by line.
  2. Use strtok to separate the contents of the line by using tab as the delimiter.


// Use an appropriate number for LINE_SIZE
#define LINE_SIZE 200
char line[LINE_SIZE];

if ( fgets(line, sizeof(line), read_file) == NULL )
{
   // Deal with error.
}

Person temp;
char* token = strtok(line, "\t");
if ( token == NULL )
{
   // Deal with error.
}
else
{
   // Copy token at most the number of characters
   // temp.name can hold. Similar logic applies to address
   // and phone number.

   temp.name[0] = '\0';
   strncat(temp.name, token, sizeof(temp.name)-1);
}

token = strtok(NULL, "\t");
if ( token == NULL )
{
   // Deal with error.
}
else
{
   temp.address[0] = '\0';
   strncat(temp.address, token, sizeof(temp.address)-1);
}

token = strtok(NULL, "\n");
if ( token == NULL )
{
   // Deal with error.
}
else
{
   temp.phone[0] = '\0';
   strncat(temp.phone, token, sizeof(temp.phone)-1);
}

更新

使用一个辅助功能,code的尺寸可以减小。 (感谢@chux)

Using a helper function, the code can be reduced in size. (Thanks @chux)

// The helper function.
void copyToken(char* destination,
               char* source,
               size_t maxLen;
               char const* delimiter)
{
    char* token = strtok(source, delimiter);
    if ( token != NULL )
    {
       destination[0] = '\0';
       strncat(destination, token, maxLen-1);
    }
}

// Use an appropriate number for LINE_SIZE
#define LINE_SIZE 200
char line[LINE_SIZE];

if ( fgets(line, sizeof(line), read_file) == NULL )
{
   // Deal with error.
}

Person temp;   
copyToken(temp.name, line, sizeof(temp.name), "\t");
copyToken(temp.address, NULL, sizeof(temp.address), "\t");
copyToken(temp.phone, NULL, sizeof(temp.phone), "\n");

这篇关于读取标签在C分离文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆