读取格式文件为用C字符数组 [英] Reading formatted file into char array in C

查看:137
本文介绍了读取格式文件为用C字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题。我需要在一个文件中的内容读入C中的字符数组文件将总是被格式化为信两列,例如:

I have a very simple problem. I need to read in the contents of a file into a char array in C. The file will always be formatted as two columns of letters, e.g.:

A B
B C
E X
C D

再$ P $每一封信psents图形上,我会处理后顶点。我使用C ++和Java编程学,但我不是非常熟悉C特别。

Each letter represents a vertex on a graph, which I'll be dealing with later. I've learned programming using C++ and Java, but I'm not extraordinarily familiar with C specifically.

是什么导致我不能找出问题的事实是,该文件是多行长。我需要每个字母占据阵列中的插槽,所以在这种情况下,它会是:
数组[0] ='A',阵列[1] ='B',阵列[2] ='B',阵列[3] ='C'等上。

What's causing the problem that I can't figure out is the fact that the file is multiple lines long. I need each letter to occupy a slot in the array, so in this case it'd be: array[0] = 'A', array[1] = 'B', array[2] = 'B', array[3] = 'C' and so on.

最后,我需要的数组不包括重复的,但后来我能处理。我写了一个程序前面这学期,在从文件整数的单行阅读和它工作得很好,所以我复制大多数code,但它不是在这种情况下工作。这是我到目前为止有:

Eventually I'll need the array to not include duplicates, but I can handle that later. I wrote a program earlier this semester that read in a single line of ints from a file and it worked fine, so I copied most of that code, but it's not working in this case. Here's what I have so far:

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

int main (int argc, char *argv[])
{
   int i;
   int count = 0;
   char * vertexArray;
   char ch = '\0';

// open file
   FILE *file = fopen( argv[1], "r" );

// count number of lines in file
   while  ((ch=fgetc(file)) != EOF)
        if(ch == '\n') count++;

// numbers of vertices is twice the number of lines
   int size = count*2;

// declare vertex array
    vertexArray = (char*) calloc(size, sizeof(char));

// read in the file to the array
   for(i=0; i<size; i++)
        fscanf(file, "%c", &vertexArray[i]);

// print the array
   for(i=0; i<size; i++)
        printf("%c\n", vertexArray[i]);

   fclose( file );
}

我知道我需要被测试的文件打开和被正确和诸如此类的东西阅读,但我将在后面补充说英寸只是想在阵列中读取了。我在这种情况下,输出为8空行。任何帮助将是巨大的!

I know I need to be testing for the file opening and being read properly and whatnot, but I'll add that in later. Just trying to read in the array for now. My output in this case is 8 blank lines. Any help would be great!

推荐答案

当您完成文件的循环来计算行数,文件指针已经坐在 EOF ,所以你不会读任何东西到你的数组。充其量这将是相同的值作为最后一个字符,但它可能会告诉你一个分段错误。

When you loop through the file to count the number of lines, the file pointer is already sitting at EOF, so you won't be reading anything into your array. At best it'll be the same value as your last character, but it'll probably show you a segmentation fault.

您想要做的是什么

rewind(file);

您之前

//read file into the array

,那么您会开始在文件的开头。
另外,我不知道龟etc 如何处理行结束,因为通常有一个尾随'\\ 0'坐那里。你可以这样做的另一种方法是使用的fscanf 像这样

then you'll be starting at the beginning of the file. Also, I'm not sure how fgetc handles end of lines, since there's usually a trailing '\0' sitting there. Another way you could do this is to use fscanf like so

i = 0;
while (!feof(file)){
   fscanf(file, "%c %c", &vertexArray[i], &vertexArray[i+1]);
   i++;
}

功能的feof(FILE *)检查是否 EOF 标志已经设置,并停止一旦你打 EOF

The function feof(FILE *) checks if the EOF flag has been set, and stops once you hit EOF.

更新:我想,如果你定义 CH INT CH ,它应该工作。看看这个线程。

UPDATE: I think if you define ch as int ch, it should work. Have a look at this thread.

这是code为我工作:

This is the code that worked for me :

int main (int argc, char *argv[])
{
    int i;
    int count = 0;
    char * vertexArray;
    int ch, size;

    FILE *file;
    file = fopen(argv[1], "r");

    while ((ch = fgetc(file) != EOF))
       count++;

    size = count*2;
    vertexArray = (char*) calloc(size, sizeof(char));
    rewind(file);

    for(i=0; i<size; i++)
       fscanf(file, "%c ", &vertexArray[i]);

    for(i=0; i<size; i++)
       fprintf(stderr, "%c\n", vertexArray[i]);

    fclose(file);

}

新修改注意在的fscanf空间(文件%C,&安培; vertexArray [I]); 。这告诉 C 要读字符后跳过所有空格。如果没有空间,它会读取空格作为字符为好。这应该修复它。

NEW EDIT Notice the space after fscanf(file, "%c ", &vertexArray[i]);. That tells C that you want to skip all the white spaces after reading the character. Without the space, it'll read the whitespace as a character as well. This should fix it.

这篇关于读取格式文件为用C字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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