Еще в процессе обучения. Я сделал небольшую программу, которая будет:
- Создайте файл с пользовательскими входами
- Прочитать файл
- Заполните структуру из файла, отсортируйте и создайте новый файл "имя sorted.txt"
это мои 3 случая. Проблема возникает, когда я ввожу кириллический текст и пытаюсь заполнить структуру, отсортировать и создать новый файл (используя вариант 3:...). Структура не заполняется, если я правильно провел тест. Но он правильно создает и читает файл (случай 1,2)
Программа делает все правильно, если я использую только латиницу. Мой код, если ниже любая помощь приветствуется...
#include "stdafx.h"
#include "iostream"
#include <fstream>
#include "string"
#include <sys/stat.h>
#include<clocale>
#include <Windows.h>
using namespace std;
#define MAX 10000// max number of students per list
//void sortStruct(); sorting structure
//void swap(studentStruct , studentStruct ); helping with sort function
//void readFile (string );reading a .txt file (ex. Saved list)
//void printStruct(); printing the struct
//void fillStruct(string ); filling a struct from a chosen file, helpin with a sort
//void calculateGrade(ofstream , int ); calcuating average grade, under witch we sort
//void writeToFile(ofstream , string ,float ); writing to a .txt file
//void printMenu(); printing a menu to chose from
//for sorting the list
struct studentStruct{
string name;
float grade;
}student[MAX];
static void printMenu()
{
//setlocale(LC_ALL, "Russian");
cout<<"MENU"<<endl<<endl;
cout<<"1. Добавить новые записи в файл или создать новый файл"<<endl;
cout<<"2. Распечатайте файл на терминал"<<endl;
cout<<"3. Сделать отсортированный версию текстового файла"<<endl;
cout<<"4. Bыход"<<endl<<endl;
cout<<"Выберите (цифры) : ";
}
inline bool exists_test (const std::string& name) {
ifstream f(name.c_str());
if (f.good()) {
f.close();
return true;
} else {
f.close();
return false;
}
}
void writeToFile(ofstream &outputFile, string name,float grade)
{
outputFile<<name<<" "<<grade<<endl;
cout << "Готово!\n";
}
void calculateGrade(ofstream &outputFile, int numProf)
{
float sum;
string name,last;
cout<<endl<<endl;
cout<<"Введите имя студента : ";
cin.sync();
getline(cin,name);
cout<<endl;
for(int i=0; i<numProf;i++)
{
cout<<"Введите "<<i+1<<". оценку (цифры) : ";
cin>>sum;
sum+=sum;
}
float grade=sum/numProf;
writeToFile(outputFile, name,grade);
}
static void fillStruct(string FileName)
{
ifstream rdFile; //Input Object
bool i;
i=exists_test(FileName);
if(i=true)cout<<"IMA";
rdFile.open(FileName);
for(int i=0; i<MAX;i++)
{
rdFile>>student[i].name;
rdFile>>student[i].grade;
}
rdFile.close();
}
static void printStruct()
{
for(int i=0;i<MAX;i++){
if(student[i].grade!=0)
cout<<student[i].name<<" "<<student[i].grade<<endl;}
cout<<"Готово!"<<endl;
}
static void readFile (string FileName)
{
ifstream rdFile; //Input Object
rdFile.open(FileName);
cout<<endl;
string n;
if (!rdFile)
{
cout<<"File could not be open...";exit(1);
}
else
{
while (rdFile && getline(rdFile, n))
{
if (n.length() == 0)continue;
cout<<n<<endl;
}
}
rdFile.close();
cout<<endl<<"Готово!"<<endl;
system("PAUSE");
}
void swap(studentStruct &x, studentStruct & y)
{
studentStruct temp;
temp = x;
x = y;
y = temp;
}
static void sortStruct()
{
for (int i = 1; i < MAX; i++)
{
for (int j = 0; j < MAX-i; j++)
{
if (student[j].grade < student[j+1].grade)
{
swap(student[j].grade, student[j+1].grade);
swap(student[j].name, student[j+1].name);
}
}
}
}
static void writeStructToFile(string outputFile)
{
ofstream rdFile;
rdFile.open(outputFile);
for(int i=0; i<MAX; i++)
{
if(student[i].grade!=0)
{
rdFile<<student[i].name<<" "<<student[i].grade<<endl;
}
}
rdFile.close();
cout<<"Готово!"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
bool check;
int flag=0,numProf,newEntreies;
string FileName,tempName,n;
ofstream outputFile;
ifstream nnn;
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
do
{
system("CLS");
printMenu();
cin>>flag;
//while(flag!=1||flag!=2||flag!=3||flag!=4){
//system("CLS");
//printMenu();
//cin>>flag;}
switch(flag)
{
case 1 :
if(FileName=="exit") {cout<<"Program will exit now..."<<endl; flag=-2;break;}
else if(FileName=="menu")break;
cout<<"Выберите имя для нового текстового файла (буквы и цифры): ";
cin.sync();
getline(cin,FileName);
cout<<"Введите число профессоров (цифры): ";
cin>>numProf;
cout<<"Введите число студентов (цифры) : ";
cin>>newEntreies;
tempName=FileName+".txt";
check=exists_test(tempName);
if(check==true)
outputFile.open(tempName, std::ios_base::app);
else
outputFile.open(tempName);
if(outputFile.fail())
{ //if opening fails then end program
cout << "Input file opening failed." << endl;
exit(1);
} //exit function from cstdlib library
system("CLS");
for(int i=0; i<newEntreies;i++)
{
cout<<FileName<<" "<<numProf<<" "<<newEntreies<<endl<<endl;
cout<<"Добавление "<<i+1<<". студента...";
calculateGrade(outputFile,numProf);
system("CLS");
}
outputFile.close();
cout<<"Нажмите любую клавишу для продолжения..."<<endl;
break;
case 2 :
if(FileName=="exit") {cout<<"Program will exit now..."<<endl;flag=-2;break;}
else if(FileName=="menu")break;
cout<<"Введите имя файла: ";
cin.sync();
getline(cin,FileName);
system("cls");
cout<<"Чтение из файла "<<FileName<<".txt"<<endl;
tempName=FileName+".txt";
readFile(tempName);
cout<<"Нажмите любую клавишу для продолжения..."<<endl;
break;
case 3 :
if(FileName=="exit") {flag=-2;break;}
else if(FileName=="menu")break;
system("CLS");
cout<<endl<<"Изготовление отсортированного файла..."<<endl<<endl;
cout<<"Выберите файл .txt(буквы и цифры): ";
cin.sync();
getline(cin,FileName);
tempName=FileName+" SORTED.txt";
fillStruct(FileName+".txt");
sortStruct();
writeStructToFile(tempName);
cout<<endl<<endl<<"Отсортировано версия "<<FileName<<" Готово!"<<endl<<endl;
cout<<"Нажмите любую клавишу для продолжения..."<<endl;
system("PAUSE");
break;
case 4: cout<< "Program will exit now..."<<endl; flag=-2;break;
default :cout<<"Program will exit now..."<<endl;flag=-2;break;
}
}while(flag!=-2);
system ("PAUSE");
return 0;
}
std::string
, основанный наchar
, предназначен только для хранения текста ASCII. Вам нужноstd::wstring
. - person dandan78   schedule 17.03.2015wstring
— это хороший шаг вперед, но консоль Windows, как известно, сложна для использования с Unicode. - person MSalters   schedule 17.03.2015fillStruct()
пытается безоговорочно прочитать 10000 записей, почему вы не используете тот же шаблон, что и вreadFile()
? - person Slava   schedule 17.03.2015using namespace std;
злой - person Slava   schedule 17.03.2015std::cout
, является иллюзией. - person Slava   schedule 17.03.2015std::string
- person Slava   schedule 17.03.2015