C / C ++读取和写入长字符串的文件 [英] C/C++ reading and writing long strings to files

查看:164
本文介绍了C / C ++读取和写入长字符串的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我格式化这样的城市的名单:

  {镇,...},
{...},
...

阅读并建立每个乡镇和创建town1,town2,工程....
问题是,当我把它输出,1号线工程{镇,...},但第二行崩溃。
任何想法,为什么?

我有[区域] [镇](EXCEL表)。

因此​​,通过多少个乡镇每个地区重复都在上面。
每个文件都有每行1区/镇。

judete包含每个区域重复1次。

  AB
SD
个人计算机
....

orase包含城镇名单。

  town1
town2
....

orase指数包含每个城市的区域

  AB
AB
AB
AB
SD
SD
SD
个人计算机
个人计算机
...

我想这样{town1,town2,...}每一行(行5)的输出包含在同一行(行5)属于区域从judete镇。

下面是我的code:

 #包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;炭judet [100] [100];
烧焦orase [50] [900000];
炭奥拉斯[100],CEVA [100];无效的主要()
{    INT I = 0,NR;
    FILE * judete,*指数,* ORASE,*输出;
    judete =的fopen(judete.txt,室温);
    指数=的fopen(orase-index.txt,室温);
    ORASE =的fopen(orase.txt,室温);
    输出= FOPEN(output.txt的,重量);    而(!的feof(judete))
    {
        与fgets(judet [Ⅰ],100,judete);
        我++;
    }    NR = I;
    TMP的char [100];
    诠释,其中= 0;    对于(i = 0; I< NR;我++)
        的strcpy(orase [I]中,);    而(!的feof(指数))
    {
        与fgets(TMP,100指数);
        对于(i = 0; I< NR;我++)
        {
            如果(的strstr(judet [I],TMP))
            {
                与fgets(奥拉斯,100,ORASE);
                strcat的(CEVA,\\);
                奥拉斯[strlen的(奥拉斯)-1] ='\\ 0';
                strcat的(CEVA,奥拉斯);
                的strcat(CEVA,\\,);
                strcat的(orase [I],CEVA);
                打破;
            }        }
    }
    烧焦了[900000]。    对于(i = 0; I< NR;我++)
    {
        的strcpy(出来,);
        strcat的(出,{);
        strcat的(出,orase [I]); //这里失败
        fprintf中(输出,%S} \\ n,出来);
    }}

结果我从运行code得到的是:


  

在0x00D4F7A9(msvcr110d.dll)在orase-judete.exe未处理的异常:0000005:访问冲突写入位置0x00A90000



解决方案

您没有明确orase阵列,东阳你的循环

 为(I-0; I< NR;我++)
    的strcpy(orase [I]中,);

错误(' - '而不是'=')执行0次

I have a list of cities that I'm formatting like this:

{town, ...}, 
{...},
...

Reading and building each town and creating town1, town2,.... works The problem is when I output it, 1st line works {town, ...}, but the second line crashes. Any idea why?

I have [region] [town] (excel table).

So each region repeats by how many towns are in it. Each file has 1 region/town per line.

judete contains each region repeated 1 time.

AB
SD
PC
....

orase contains the towns list.

town1
town2
....

orase-index contains the region of each town

AB
AB
AB
AB
SD
SD
SD
PC
PC
...

I want an output like this {"town1", "town2", ...} and each row (row 5) contains the town that belong to the region from judete at the same row (row 5).

Here's my code:

#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);
    }

}

The result I get from running the code is:

Unhandled exception at 0x00D4F7A9 (msvcr110d.dll) in orase-judete.exe: 0xC0000005: Access violation writing location 0x00A90000.

解决方案

You don't clear orase array, beacause your loop

for(i-0;i<nr;i++)
    strcpy(orase[i],"");

by mistake ('-' instead of '=') executes 0 times.

这篇关于C / C ++读取和写入长字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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