【发布时间】:2014-11-10 12:23:18
【问题描述】:
您好,我正在介绍 C 编程课程,因此我使用的是非常基本的代码。在这里,我只是想从主字符串中获取逗号矩阵。但是,当我尝试运行该程序时,它一直在我身上崩溃,我不知道我的问题是什么。我能够正确使用 fgets 功能,所以我认为它仍然可以正常工作。
CD Data.txt 文件
Eagles, Hotel California, 1976, Rock, 4
The Fratellis, Costello Music, 2006, Garage Rock, 5
Awolnation, Megalithic Symphony, 2011, Indie Rock, 5
Lindsey Stirling, Lindsey Stirling, 2012, Classical Crossover, 5
Arctic Monkeys, AM, 2013, Indie Rock, 4
计划
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define row 1000
#define column 1000
void getCommas(char str[], int commas[])
{
int flag, count, index;
count = 0;
index = 0;
flag = 1;
while(flag = 1)
{
if(str[count] = ',')
{
commas[index] = count;
index = index + 1;
}
count = count + 1;
if(str[count] = '\0')
{
flag = 0;
}
}
}
int main()
{
int i;
char CdInfo[row][column];
int Index[row][column];
FILE *fp;
fp = fopen("CD Data.txt","r");
for(i=0; i<5; i++)
{
fgets(CdInfo[i], sizeof CdInfo, fp);
//printf("%s\n",CdInfo[i]);
}
for (i=0; i<5; i++)
{
getCommas(CdInfo[i], Index[i]);
}
fclose(fp);
return 0;
}
【问题讨论】:
-
首先,将此
if(str[count] = ',')更改为if(str[count] == ','),将此if(str[count] = '\0')更改为此if(str[count] == '\0')。 -
另外,将
fgets(CdInfo[i], sizeof CdInfo, fp)改为fgets(CdInfo[i], sizeof CdInfo[i], fp) -
谢谢巴拉克!这有很大帮助,但是如果我将 CdInfo 更改为 CdInfo[i] 它并没有给我想要的东西,CdInfo 就像它一样工作正常。
-
所有这一切都意味着你要么有一个非常糟糕的编译器(甚至比 1990 年的 Turbo C 还要糟糕,它会为此代码产生警告),或者更有可能的是,你禁用了基本的编译器警告。
-
是否有重置按钮或默认设置?我几乎不知道如何使用 CodeBlocks,我还没有进入过设置。那里的语言对我来说是胡言乱语。