我有我格式化这样的城市列表:C/C++读取和写入长字符串的文件
{town, ...},
{...},
...
阅读并建立每个乡镇和创建town1,town2,工程.... 问题是当我输出它时,第一行工作{镇,...},但第二行崩溃。 任何想法为什么?
我有[地区] [城镇](excel表)。
因此,每个区域重复有多少个城镇。 每个文件每行有1个区域/城镇。
judete包含每个区域重复1次。
AB
SD
PC
....
orase包含城镇列表。
town1
town2
....
orase指数包含每个镇
AB
AB
AB
AB
SD
SD
SD
PC
PC
...
我想这样{ “town1”, “town2”,...}每行输出的区域(第5行)包含属于同一行judete的地区的城镇(第5行)。
这里是我的代码:
#include<stdio.h>
#include<string.h>
char judet[100][100];
char orase[50][900000];
char oras[100], ceva[100];
void main()
{
int i=0, nr;
FILE *judete, *index, *ORASE, *output;
judete = fopen("judete.txt", "rt");
index = fopen("orase-index.txt", "rt");
ORASE = fopen("orase.txt", "rt");
output = fopen("output.txt", "wt");
while(!feof(judete))
{
fgets(judet[i], 100, judete);
i++;
}
nr = i;
char tmp[100];
int where=0;
for(i=0;i<nr;i++)
strcpy(orase[i],"");
while(!feof(index))
{
fgets(tmp, 100, index);
for(i=0;i<nr;i++)
{
if(strstr(judet[i], tmp))
{
fgets(oras, 100, ORASE);
strcat(ceva, "\"");
oras[strlen(oras)-1]='\0';
strcat(ceva, oras);
strcat(ceva, "\", ");
strcat(orase[i], ceva);
break;
}
}
}
char out[900000];
for(i=0;i<nr;i++)
{
strcpy(out, "");
strcat(out, "{");
strcat(out, orase[i]); //fails here
fprintf(output, "%s},\n", out);
}
}
结果我从运行代码得到的是:
未处理的异常在0x00D4F7A9(msvcr110d.dll)在orase-judete.exe:0000005:访问违规写入地址0x00A90000。
它是如何崩溃的?如果通过'gdb'运行它,会发生什么? 另外,不要使用strcat/strcpy。使用'n'版本('strncpy'等) –
你可以给一个judete.txt的小样本吗? –
@OliverMatthews'strncpy()'不比'strcpy()'更好,它只是有一个不同的谬误。 – 2013-08-22 13:46:15