1

次のようにフォーマットしている都市のリストがあります。

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

各町を読んで構築し、町1、町2、....を作成する問題は、出力すると、1行目は{町、...}が機能しますが、2行目はクラッシュします。理由はありますか?

[地域] [町] (Excel テーブル) があります。

したがって、各地域は、その中にある町の数だけ繰り返されます。各ファイルには、1 行に 1 つの地域/町があります。

judete には、各領域が 1 回繰り返されます。

AB
SD
PC
....

orase には町のリストが含まれています。

town1
town2
....

orase-index には各町の地域が含まれています

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

この {"town1", "town2", ...} のような出力が必要で、各行 (5 行目) には同じ行 (5 行目) の judete の地域に属する町が含まれています。

これが私のコードです:

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

}

コードを実行した結果は次のとおりです。

orase-judete.exe の 0x00D4F7A9 (msvcr110d.dll) で未処理の例外: 0xC0000005: アクセス違反書き込み場所 0x00A90000。

4

3 に答える 3

4

ループのため、配列をクリアしません

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

誤って ('=' の代わりに '-') を実行すると、0 回実行されます。

于 2013-08-22T13:50:02.673 に答える
3

C を書くのか C++ を書くのかを決めることから始める必要があると思います。これに両方のタグを付けましたが、コードは純粋な C のように見えます。C++ コンパイラはほとんどの C を受け入れますが、その結果はほとんどの人が理想的な C++ と考えるものではありません。

あなたは C++ としてタグ付けしたので、実際に C++ コードが必要な (または問題なく使用できる) と仮定します。適切に作成された C++ コードは、現在の C コードとは十分に異なるため、コードを 1 行ずつ書き直すよりも、最初からやり直す方がおそらく簡単です。

ただし、それを行う際に私が目にする差し迫った問題は、出力として必要なものを実際に指定していないことです。今のところ、出力の各行を次のようにしたいと仮定します"{" <town> "," <town> "}"

その場合は、出力がjudeteファイルにまったく依存していないように見えることに注意することから始めます。oraseとはorase-index完全に適切なようです。そのために、コードは次のようになります。

#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
#include <vector>

// a class that overloads `operator>>` to read a line at a time:
class line { 
    std::string data;
public:
    friend std::istream &operator>>(std::istream &is, line &l) { 
        return std::getline(is, l.data);
    }
    operator std::string() const { return data; }
};

int main() {
    // open the input files:
    std::ifstream town_input("orase.txt");
    std::ifstream region_input("orase-index.txt");

    // create istream_iterator's to read from the input files. Note
    // that these iterate over `line`s, (i.e., objects of the type
    // above, so they use its `operator>>` to read each data item).
    //
    std::istream_iterator<line> regions(region_input), 
                                towns(town_input), 
                                end;

    // read in the lists of towns and regions:
    std::vector<std::string> town_list {towns, end};
    std::vector<std::string> region_list {regions, end};

    // write out the file of town-name, region-name:
    std::ofstream result("output.txt");
    for (int i=0; i<town_list.size(); i++) 
        result << "{" << town_list[i] << "," << region_list[i] << "}\n";
}

これは C++ であるため、通常、コンパイラがソースを正しく認識するsomething.cppのではなく、ソースを名前を付けて保存する必要があります。something.c

編集:コメントで指定した新しい要件に基づいて、明らかにこれに近いものが必要です:

#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
#include <vector>
#include <map>

// a class that overloads `operator>>` to read a line at a time:
class line { 
    std::string data;
public:
    friend std::istream &operator>>(std::istream &is, line &l) { 
        return std::getline(is, l.data);
    }
    operator std::string() const { return data; }
};

int main() {
    // open the input files:
    std::ifstream town_input("orase.txt");
    std::ifstream region_input("orase-index.txt");

    // create istream_iterator's to read from the input files. Note
    // that these iterate over `line`s, (i.e., objects of the type
    // above, so they use its `operator>>` to read each data item).
    //
    std::istream_iterator<line> regions(region_input), 
                                towns(town_input), 
                                end;

    // read in the lists of towns and regions:
    std::vector<std::string> town_list (towns, end);
    std::vector<std::string> region_list (regions, end);

    // consolidate towns per region:
    std::map<std::string, std::vector<std::string> > consolidated;
    for (int i = 0; i < town_list.size(); i++)
        consolidated[region_list[i]].push_back(town_list[i]);

    // write out towns by region
    std::ofstream output("output.txt");
    for (auto pos = consolidated.begin(); pos != consolidated.end(); ++pos) {
        std::cout << pos->first << ": ";
        std::copy(pos->second.begin(), pos->second.end(),
            std::ostream_iterator<std::string>(output, "\t"));
        std::cout << "\n";
    }       
}
于 2013-08-22T14:45:55.210 に答える