我有我格式化這樣的城市列表: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