Запись в файл

Тема в разделе "MS Visual C++", создана пользователем enzo, 21 май 2007.

Статус темы:
Закрыта.
  1. enzo

    enzo Гость

    Почему после считывания из файла( ф-ция Read() ) не работает запись в файл ( AddToFile() )??
    ошибка вылазеет :
    ---------------------------
    Microsoft Visual C++ Debug Library
    ---------------------------
    Debug Error!

    Program: ...icrosoft Visual Studio\MyProjects\Proga\Debug\Proga.exe



    abnormal program termination



    (Press Retry to debug the application)
    ---------------------------
    Прервать Повтор Пропустить
    ---------------------------
    Код (Text):
    #include "stdafx.h"
    #include <stdlib.h>
    #include <conio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    struct Node_Stud
    {
    string name;
    string fam;
    string date;
    int num;
    int sum;
    int tel;
    Node_Stud *next;
    };
    int N1=0;
    const int N=1000;
    Node_Stud *St[N];

    Node_Stud *head=0;  // yK-JIb Ha roJIoBy CnuCKa CTygeHToB
    string CHARS="QRSTUVWXYZ"; //CTpoKa noCJIegHux 10 CuMBOJIOB
    int i=0;
    void ADD(string Familiya, string Name, string Date, int school, int ball, int tel);
    void Show();
    void AddToFile();   //npoToTun funkcii zapis' v file
    void Read();    //npoToTun funkcii 4TeHue u3 faila

    /*
    -----------------------------------------------------------------------------------*/

    void main()
    {
    cout << "KypcoBa9I Pa6oTa no Kypcy uHfopMaTuKa"<<endl;
    cout << "CTygeHTa rpynnbl info-101"<<endl;
    cout << ""<<endl;
    cout << endl <<"-------------------------------------"<< endl;

    ///////////////////////////gaJIee kog napoJI9///////////////////////////////
    int a=5, i=0, tryes=1;
    char pass[10];
    char correct[10]="321";

    B:  cout << "BBeguTe napoJIb. "<< endl << "->";

    M:  a = getch();// funkcia ostanova
    if(a == 13) //
    goto N;
    cout <<"*";
    pass[i]=(char)(a);
    i++;
    goto M;
    N:  pass[i]=0;

    if(strcmp(correct, pass)==0)
    {
    cout << endl <<"BEPHO!" << endl;
    }
    else
    {
    cout << endl << "HeBepHo" << endl;
    cout << endl;
    for(int tt=0; tt<9; tt++ )
    {
    pass[tt]=(char)(' /n');//
    }

    i=0;
    tryes++;
    if(tryes>=4)
    {
    cout << "napoJIb BBegeH HeBepHo.Pa6oTa nporpaMMbl 3aBepLLIeHa" << endl;
    return;
    }

    goto B;      
    }



    //----gaJIee kog menu----//
    A:   cout << endl << endl;
    cout << " Menu:" << endl;
    cout << "1.Add" << endl;
    cout << "2.Add to file" << endl;
    cout << "3.Read from file" << endl;
    cout << "4.Show all"<<endl;
    cout << "5.Exit" << endl << endl;
    int menu;
    cout << "BBeguTe nyHKT menu :" << endl;
    cin >> menu;
    switch (menu)
    {

    case 1:
    {
    cout << endl;
    cout <<"1.Dobavlenie"<< endl;
    Node_Stud N1;
    cout << endl;
    cout <<" vvedite UM9I:"<< endl;
    cin >> N1.name;
    cout << endl;
    cout <<" vvedite familiu:"<< endl;
    cin >> N1.fam;
    cout << endl;
    cout << " vvedite datu rozhdenia:"<< endl;
    cin>> N1.date;
    cout << endl;
    cout << " vvedite nomer shkoli:"<< endl;
    cin >> N1.num;
    cout << endl;
    cout << " vvedite summu ballov : "<< endl;
    cin >> N1.sum;
    cout << endl;
    cout << " vvedite nomer telefona : "<< endl;
    cin >> N1.tel;
    ADD(N1.name, N1.fam, N1.date, N1.num, N1.sum, N1.tel);
    goto A;
    break;   
    }

    case 2:
    {
    cout << "2.Zapis' v fail:"<<endl;
    AddToFile();
    goto A;    
    }

    case 3:
    {  
    cout <<"3.Chtenie iz faila:"<<endl;
    Read();
    goto A;
    }

    case 4:
    {
    cout << "4.Prosmotr:"<<endl;
    Show();
    goto A;
    }

    case 5:
    {
    cout << "5.Rabota programmi zavershena.Press any key to exit."<<endl;
    cout << endl;
    return;
    }

    default:
    {
    cout <<"HeBePHblu BBog"<<endl; 
    break;
    }
    }
    }


    ///////////////////////////////OnpegeJIeHue funkcii go6aBJIeHu9/////////////////////////////////////
    void ADD(string Name,string Familiya , string Date, int school, int ball, int tel)
    {
    Node_Stud *NEW = new Node_Stud;
    NEW -> name = Name;
    NEW -> fam = Familiya;
    NEW -> date = Date;
    NEW -> num = school;
    NEW -> sum = ball;
    NEW -> tel = tel;
    NEW -> next= head;
    head = NEW;
    }


    ///////////////////////////////OnpegeJIeHue funkcii 3anucb B fail///////////////////////////////////
    void AddToFile()
    {  

    ofstream of1; //noTok Ha BbBog
    of1.open("D:\\my\\B.TXT",ios::trunc); //OTKPblTue noToKa na BblBog
    if(!of1)
    cout<< "error: can't open A.DAT" << endl;
    Node_Stud *current = head;
    string dd;
    dd = current->fam;
    while (current!=0)
    {
    i++;
    for (int nn=0; nn<10; nn++)
    if (dd.at(0) == CHARS.at(nn))
    of1 << i<<") ";
    of1 << "UM9I - "<< current->name << endl <<"FaMuJIu9l- "<< current->fam << endl
    << "data-"<< current->date << endl <<"HoMeP LLIKoJIbl- "<< current->num<<endl
    << "CyMMa 6aJIJIoB- " << current->sum << endl << "HoMeP TeJIefoHa- "
    << current->tel << endl;
    of1 << endl;
    current = current->next;
    }
    of1.close();
    }


    //////////////////////////////////OnpegeJIeHue funkcii npoCMoTpa//////////////////////////////////////
    void Read()
    {
    cout <<" BBeguTe uM9 faila:";
    string s0;
    cin >> s0;
    ifstream if1;
    if (s0=="A.TXT")
    if1.open("D:\\my\\A.TXT");
    else
    {
    cout <<"HeBePHoe uM9 faila"<<endl;
    }
    if(!if1)
    {
    cout<<"HeBo3Mo}I{Ho OTKPbITb"<<endl;
    exit(0);
    }

    while(!if1.eof())
    {  
    string Fam, Name, Date;
    int Sch, Sum, Phone;
    if1 >> Fam;
    if1 >> Name;
    if1 >> Date;
    if1 >> Sch;
    if1 >> Sum;
    if1 >> Phone;
    ADD(Fam, Name, Date, Sch, Sum, Phone);

    }

    cout << endl;
    if1.close();

    }

    ///////////////////////////////////////////////////////////////////////////////

    void Show()
    {

    Node_Stud *current = head;
    int i=1;
    while(current != 0)
    {

    cout << i <<"."<<"Name: "<< current ->name << endl;
    cout <<" Familia: " <<current ->fam << " " << endl;
    cout <<" Data rozhdenia: "<<current ->date << endl;
    cout <<" Nomer Shkoli: "<< current ->num<<endl;
    cout <<" Kolichestvo ballov: "<<current ->sum<<endl;
    cout <<" Telephone number: "<< current -> tel<<endl;
    current = current -> next;
    cout << endl;
    i++;
    }
    }
     
  2. enzo

    enzo Гость

    вот так работает
    Код (Text):
    void AddToFile()
    {  
    Node_Stud *current = head;
    ofstream of1; //noTok Ha BbBog
    of1.open("D:\\my\\B.TXT",ios::out); //OTKPblTue noToKa na BblBog
    if(!of1)
    cout<< "error: can't open A.DAT" << endl;  
    while (current!=0)
    {
    i++;           




    of1 << i<<") ";
    of1 << "UM9I - " << current->name << endl << "FaMuJIu9l- " << current->fam
    << endl
    << "data-"<< current->date << endl << "HoMeP LLIKoJIbl- " << current->num                                   << endl
    << "CyMMa 6aJIJIoB- " << current->sum << endl << "HoMeP TeJIefoHa- "
    << current->tel << endl;           
    of1 << endl;

    current = current->next;
    }
    of1.close();
    }
    но мне нужно, чтобы первый символ строки фамилии (current->fam) сравнивался с массивом CHARS, и если первый символ фамилии == CHARS(i=0....9) то далее - запись в файл.
     
Загрузка...
Статус темы:
Закрыта.

Поделиться этой страницей