2013-03-09 122 views
3

让说我有一个文件看起来像这样从文本文件中读取字符串和整

51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers 
77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers 
31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping 
52.03 12.02 12.04 22.00 31.98 61.97 Big Bank 
44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping 
93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs 

我想用的fscanf把浮法阵列和字号码的阵列,以读取该文件的字字符串。但是,经过几个小时的艰苦思考,我仍然无法弄清楚如何解决这个问题。

void dataInsert (COMPANY* company1, COMPANY* company2, COMPANY* company3, COMPANY* company4, COMPANY* company5, COMPANY* company6) 
{ 
//Function Declaration 
FILE* spData; 
float number; 
char* name[20]; 

//Statement 
if ((spData = fopen("dataFile","r")) == NULL) 
{ 
    fprintf(stderr, "ERROR OPENING!!"); 
    exit (1); 
} 

int i = 0; 
int numCount = 0; 
int lineCount = 0; 
while (fscanf(spData, "%f", &number) != EOF) 
{ 
    if(isdigit(number)) 
    { 
     if (lineCount == 0) 
     { 
      company1 -> stock_price[i] = number; 
     } 
     else if (lineCount == 1) 
     { 
      company2 -> stock_price[i] = number; 
     } 
     else if (lineCount == 2) 
     { 
      company3 -> stock_price[i] = number; 
     } 
     else if (lineCount == 3) 
     { 
      company4 -> stock_price[i] = number; 
     } 
     else if (lineCount == 4) 
     { 
      company5 -> stock_price[i] = number; 
     } 
     else if (lineCount == 5) 
     { 
      company6 -> stock_price[i] = number; 
     } 

     numCount++; 
     i++; 
     if (numCount == 6) 
     { 
      lineCount++; 
      numCount = 0; 
      i = 0; 
     } 
    } 
}//while 
fclose (spData); 
}//dataInsert 

我不知道如何处理每行末尾的字符串。我想把这些字符串放在结构公司 - >名称[10]中。这些数据都在一个文本文件中。

+1

您确定要读出的值作为_integers_而不是_floating point_?另外,请告诉我们[你曾尝试](http://mattgemmell.com/2008/12/08/what-have-you-tried/)。 – 2013-03-09 02:02:21

+0

这些实际的'
'标签在文件中?这是文件的XML或HTML或什么?也许你应该使用解析库?如果该文件与您在此显示的格式完全相同,则可以使用C语言对其进行解析,但脚本语言会更容易一些...... Python将是我的选择。 – steveha 2013-03-09 02:05:03

+2

你的文件是否总是这样组织?在字符串之前有相同数量的数字?或者你是否必须检测你正在阅读的是数字还是字符串? – 2013-03-09 02:11:14

回答

2

如果文件格式正确,可以轻松使用scanf()。这里有一些代码让你开始。我没有测试过这个,你需要填写一些缺失的东西。

#include <ctypes.h> // for isspace() 
#include <stdio.h> // for scanf(), getchar(), and EOF 

char c2d[MAX_LINES][MAX_LENGTH_STRING_PER_LINE]; 
char *pstr; 
float f2d[MAX_LINES][6]; // 6 floats per line 
float *p; 
int c, current_line_number; 
char ch; 
FILE *input; 

input = fopen(...); 
if (!input) 
    ... handle the error 

for (current_line_number = 0; ; ++current_line_number) 
{ 
    // handle each line of input 

    // first read 6 float values 
    p = f2d + current_line_number; 
    c = fscanf(input, "%f %f %f %f %f %f", p + 0, p + 1, p + 2, p + 3, p + 4, p + 5); 
    if (c != 6) 
     ... handle the error here 

    // next grab string; stop at '<' or end of line or EOF 
    pstr = c2d + current_line_number; 
    for (;;) 
    { 
     ch = fgetc(input); 
     if (ch == EOF || ch == '<' || ch == '\n') 
     { 
      *pstr = '\0'; 
      break; 
     } 
     *pstr++ = ch; 
    } 
    if (ch == '<') 
    { 
     // char was '<' so throw away rest of input line until end of line 
     for (;;) 
     { 
      if (ch == EOF || ch == '\n') 
       break; 
      ch = fgetc(input); 
     } 
    } 
    for (;;) 
    { 
     // eat up any white space, including blank lines in input file 
     if (ch == EOF || !isspace(ch)) 
      break; 
     ch = fgetc(input); 
    } 
    // once we have hit end of file we are done; break out of loop 
    if (ch == EOF) 
     break; 
} 

fclose(input); 

我没有使用scanf()阅读在该行的结束的字符串,因为它停止,当它击中的白色空间,和你的字符串值在他们的空间。

如果输入文件并非总是六个浮点值,则需要编写代码以每次调用一个浮点数,直到遇到不会作为浮点解析的东西,并且您需要花车阵列的宽度足以应付每条线路允许的最大花车数量。

祝你好运。

+0

'fscanf'被用来代替'scanf',对吧?输入不是来自stdin,而是来自文件。 – uba 2013-03-09 02:30:04

+0

当然,'fgetc()'而不是'getchar()'。我改变了答案。 – steveha 2013-03-09 02:32:03

+0

如果我使用scanf,它是否跳过每行结尾的字符串? – 2013-03-09 02:36:58

4

而不是使用fscanf我会建议使用fgets来获得该行。然后使用该行上的sscanf来获取数字值,并搜索第一个字母字符以知道字符串的起始位置(使用例如strspn)。

事情是这样的:

char line[256]; 

while (fgets(line, sizeof(line), fp) != NULL) 
{ 
    /* Get the numbers */ 
    float numbers[6]; 
    sscanf(line, "%f %f %f %f %f %f", 
     &numbers[0], &numbers[1], &numbers[2], 
     &numbers[3], &numbers[4], &numbers[5]); 

    /* Where do the numbers end... */ 
    size_t numbers_end = strspn(line, "1234567890. \t"); 

    /* And get the name */ 
    char *name = line + numbers_end; 

    /* Do something with the numbers and the name */ 
} 
+0

感谢您的回复,但不幸的是,我不能使用sscanf,因为这是学校作业的一部分,所以我必须根据我在课堂上学到的内容来做。 – 2013-03-09 05:29:32

+0

@ProgrammingNerd永远不要害怕“跳出框框思考”......你可以做两个解决方案,一个遵循字母_赋值,然后列出可能的缺点,然后列出一个解决方案,但没有缺点。 – 2013-03-09 15:03:10

+0

@ProgrammingNerd不是我说我的解决方案本身没有缺点,所有的解决方案都有利弊,包括我的。 – 2013-03-09 15:04:34

相关问题