Estou tentando manipular arquivos em C++, utilizo o Visual Studio e o arquivo encontra-se dentro da pasta Source Files, preciso alterar o conteúdo dentro do arquivo, por ex.:
Teste.txt (nome do arquivo)
Dentro do arquivo contem 10 linhas com diversas palavras.
Quando o usuario desejar alterar alguma linha, o programa deverá encontrar a posição da mesma e alterar seu conteúdo, porém não estou conseguindo fazer isso, alguém tem alguma dica?
Segue abaixo o código.
#include<iostream>
#include<fstream>
#include<string>
#include <ctime>
using namespace std;
struct Significados{
string traducao[5];
};
struct inserir{
string palavra;
Significados traducoes;
};
void swap(int pos)
{
ofstream openfile;
string a,b,c,d,e,f,g;
int pos_aux=0;
openfile.open("teste.txt",ios::in|ios::erase_event);
ifstream get("dic.txt",ios::in);
while (!get.eof())
{
pos_aux++;
if (pos_aux != pos)
{
get>>a>>b>>c>>d>>e>>f>>g;
openfile << a << " " << b << " "<< c << " "<< d << " "<< e << " "<< f << " "<< g << "$%\n";
}
}
}
string conversao_min_max(string palavra)
{
char teste;
for (int i=0; i < palavra.length(); i++)
{
if ((palavra[i]>='a')&&(palavra[i]<='z'))
{
teste = palavra[i];
teste = teste-('a'-'A');
palavra[i]= teste;
}
}
return palavra;
}
void busca_significado(string p)
{
bool teste=true;
string a,b,c,d,e,f,g;
ifstream get("dic.txt",ios::in);
while ((!get.eof())&&(teste!=false))//enquanto não for fim do arquivo
{
get>>a>>b>>c>>d>>e>>f>>g;
if (c == "$%")
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(d == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " e " << c << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(e == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << " e " << d << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(f == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << " e " << e << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == e )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(g == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << ", " << e << " e " << f << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == e )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == f )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
}
if (teste == true )
cout << "não encontrado! | Not found! " << endl;
}
int verifica_existencia(string p )
{
bool teste = true;
string a,b,c,d,e,f,g;
int pos=0;
ifstream get("dic.txt",ios::in);
while ((!get.eof())&&(teste!=false))//enquanto não for fim do arquivo
{
pos++;
get>>a>>b>>c>>d>>e>>f>>g;
if (c == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(d == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " e " << c << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(e == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << " e " << d << " . " << endl;
teste=false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste=false;
break;
}
}
else if(f == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << " e " << e << " . " << endl;
teste = false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == e )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
}
else if(g == "$%" )
{
if (p == a )
{
cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << ", " << e << " e " << f << " . " << endl;
teste = false;
break;//para não ocorrer repetição do significado.
}
else if(p == b )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == c )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == d )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == e )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
else if(p == f )
{
cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl;
teste = false;
break;
}
}
}
return pos;
}
void menu()
{
system("color 37");
cout<<".:. Dicionario PT EN | EN PT Dictionary .:."<<endl<< endl;
cout<<"1.Inserir nova palavra | Insert new word."<<endl;
cout<<"2.Mostrar traducao | Show meaning."<<endl;
cout<<"3.Creditos | Credits."<<endl;
cout<<"0.Sair | Exit."<< endl << endl;
}
void insercao()
{
inserir dicionario;
string aux;
bool teste = false;
int pos=0;//posicao de que linha encontra-se a palavra
char moresignificados;
int controle=0;
ofstream openfile;
openfile.open("dic.txt",ios::app);
cout << "Insira uma nova palavra em ingles | Enter a new word in English: ";
cin >> dicionario.palavra;
aux = conversao_min_max(dicionario.palavra);
dicionario.palavra= aux;
pos =verifica_existencia(dicionario.palavra);
if (pos > sizeof("dic.txt"))
{
cout << "Insira seu significado em portugues | Enter the meaning in Portuguese: ";
cin>> dicionario.traducoes.traducao[0];
aux = conversao_min_max(dicionario.traducoes.traducao[0]);
dicionario.traducoes.traducao[0]= aux;
for (int i=1; i<5; i++)
{
cout << "Conhece outro significado? ";
cout << "Pressione Y ou S para informar mais um significado. ";
cin >> moresignificados;
if ((moresignificados == 'y')||(moresignificados =='Y')||((moresignificados =='s'))||(moresignificados =='S'))
{
cout << "Insira seu significado em portugues | Enter the meaning in Portuguese: ";
cin>>dicionario.traducoes.traducao[i];
aux = conversao_min_max(dicionario.traducoes.traducao[i]);
dicionario.traducoes.traducao[i] = aux;
}
else
{
controle = i;
break;
}
controle=i;
}
openfile << endl << dicionario.palavra;//grava a palavra no arquivo
for (int i=0; i<=controle; i++)
{
openfile << " " << dicionario.traducoes.traducao[i];
}
for (int i=controle; i<=5; i++)
{
openfile << " $%";
}
openfile.close();
}
else
{
cout << "Deseja alterar? Para sim, pressione S ou Y. " << endl;
cin >> moresignificados;
if ((moresignificados == 'y')||(moresignificados =='Y')||((moresignificados =='s'))||(moresignificados =='S'))
{
swap(pos);
cout << "under construction... " << endl;
system("pause");
}
}
}
void dicionario()
{
int ch;
string aux;
bool test = true;
do
{
menu();
cout << "Opcao escolhida | Option chosen: ";
cin >> ch;
if (ch == 1)
{
insercao();
}
else if(ch == 2)
{
string word;
cout<<"Informe a palavra a ser traduzida | Enter the word to be translated: ";
cin>>word;
aux = conversao_min_max(word);
word = aux;
busca_significado(word);
system("pause 2s");
}
else if (ch == 3)
{
cout<< "Trabalho de PI. | Work to Interdiciplinar Project. " << endl;
system("pause 2s");
}
else if (ch == 0 )
{
cout << endl << "Saindo, aguarde. | Leaving, wait. " << endl<< endl;
break;
test = false;
}
else
{
cout << "Opcao invalida! Por favor, repita o processo. " << endl << "Invalid choice! Please repeat the process." << endl << endl;
system("pause 2s");
}
system("cls");
}while(test !=false);
}
void main()
{
dicionario();
}
Pergunta
Alexandre Barsalini
Boa tarde a todos.
Estou tentando manipular arquivos em C++, utilizo o Visual Studio e o arquivo encontra-se dentro da pasta Source Files, preciso alterar o conteúdo dentro do arquivo, por ex.:
Teste.txt (nome do arquivo)
Dentro do arquivo contem 10 linhas com diversas palavras.
Quando o usuario desejar alterar alguma linha, o programa deverá encontrar a posição da mesma e alterar seu conteúdo, porém não estou conseguindo fazer isso, alguém tem alguma dica?
Segue abaixo o código.
#include<iostream> #include<fstream> #include<string> #include <ctime> using namespace std; struct Significados{ string traducao[5]; }; struct inserir{ string palavra; Significados traducoes; }; void swap(int pos) { ofstream openfile; string a,b,c,d,e,f,g; int pos_aux=0; openfile.open("teste.txt",ios::in|ios::erase_event); ifstream get("dic.txt",ios::in); while (!get.eof()) { pos_aux++; if (pos_aux != pos) { get>>a>>b>>c>>d>>e>>f>>g; openfile << a << " " << b << " "<< c << " "<< d << " "<< e << " "<< f << " "<< g << "$%\n"; } } } string conversao_min_max(string palavra) { char teste; for (int i=0; i < palavra.length(); i++) { if ((palavra[i]>='a')&&(palavra[i]<='z')) { teste = palavra[i]; teste = teste-('a'-'A'); palavra[i]= teste; } } return palavra; } void busca_significado(string p) { bool teste=true; string a,b,c,d,e,f,g; ifstream get("dic.txt",ios::in); while ((!get.eof())&&(teste!=false))//enquanto não for fim do arquivo { get>>a>>b>>c>>d>>e>>f>>g; if (c == "$%") { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(d == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " e " << c << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(e == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << " e " << d << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(f == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << " e " << e << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == e ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(g == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << ", " << e << " e " << f << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == e ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == f ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } } if (teste == true ) cout << "não encontrado! | Not found! " << endl; } int verifica_existencia(string p ) { bool teste = true; string a,b,c,d,e,f,g; int pos=0; ifstream get("dic.txt",ios::in); while ((!get.eof())&&(teste!=false))//enquanto não for fim do arquivo { pos++; get>>a>>b>>c>>d>>e>>f>>g; if (c == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(d == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << " e " << c << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(e == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << " e " << d << " . " << endl; teste=false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste=false; break; } } else if(f == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << " e " << e << " . " << endl; teste = false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == e ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } } else if(g == "$%" ) { if (p == a ) { cout<<"O termo pesquisado encontra se em ingles e em portugues significa: "<<b << ", " << c << ", " << d << ", " << e << " e " << f << " . " << endl; teste = false; break;//para não ocorrer repetição do significado. } else if(p == b ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == c ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == d ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == e ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } else if(p == f ) { cout << "The searched term is in Portuguese and in English meaning: " << a << ". " << endl; teste = false; break; } } } return pos; } void menu() { system("color 37"); cout<<".:. Dicionario PT EN | EN PT Dictionary .:."<<endl<< endl; cout<<"1.Inserir nova palavra | Insert new word."<<endl; cout<<"2.Mostrar traducao | Show meaning."<<endl; cout<<"3.Creditos | Credits."<<endl; cout<<"0.Sair | Exit."<< endl << endl; } void insercao() { inserir dicionario; string aux; bool teste = false; int pos=0;//posicao de que linha encontra-se a palavra char moresignificados; int controle=0; ofstream openfile; openfile.open("dic.txt",ios::app); cout << "Insira uma nova palavra em ingles | Enter a new word in English: "; cin >> dicionario.palavra; aux = conversao_min_max(dicionario.palavra); dicionario.palavra= aux; pos =verifica_existencia(dicionario.palavra); if (pos > sizeof("dic.txt")) { cout << "Insira seu significado em portugues | Enter the meaning in Portuguese: "; cin>> dicionario.traducoes.traducao[0]; aux = conversao_min_max(dicionario.traducoes.traducao[0]); dicionario.traducoes.traducao[0]= aux; for (int i=1; i<5; i++) { cout << "Conhece outro significado? "; cout << "Pressione Y ou S para informar mais um significado. "; cin >> moresignificados; if ((moresignificados == 'y')||(moresignificados =='Y')||((moresignificados =='s'))||(moresignificados =='S')) { cout << "Insira seu significado em portugues | Enter the meaning in Portuguese: "; cin>>dicionario.traducoes.traducao[i]; aux = conversao_min_max(dicionario.traducoes.traducao[i]); dicionario.traducoes.traducao[i] = aux; } else { controle = i; break; } controle=i; } openfile << endl << dicionario.palavra;//grava a palavra no arquivo for (int i=0; i<=controle; i++) { openfile << " " << dicionario.traducoes.traducao[i]; } for (int i=controle; i<=5; i++) { openfile << " $%"; } openfile.close(); } else { cout << "Deseja alterar? Para sim, pressione S ou Y. " << endl; cin >> moresignificados; if ((moresignificados == 'y')||(moresignificados =='Y')||((moresignificados =='s'))||(moresignificados =='S')) { swap(pos); cout << "under construction... " << endl; system("pause"); } } } void dicionario() { int ch; string aux; bool test = true; do { menu(); cout << "Opcao escolhida | Option chosen: "; cin >> ch; if (ch == 1) { insercao(); } else if(ch == 2) { string word; cout<<"Informe a palavra a ser traduzida | Enter the word to be translated: "; cin>>word; aux = conversao_min_max(word); word = aux; busca_significado(word); system("pause 2s"); } else if (ch == 3) { cout<< "Trabalho de PI. | Work to Interdiciplinar Project. " << endl; system("pause 2s"); } else if (ch == 0 ) { cout << endl << "Saindo, aguarde. | Leaving, wait. " << endl<< endl; break; test = false; } else { cout << "Opcao invalida! Por favor, repita o processo. " << endl << "Invalid choice! Please repeat the process." << endl << endl; system("pause 2s"); } system("cls"); }while(test !=false); } void main() { dicionario(); }Valeu!
Editado por DurubAdicionar tags code [Durub]
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.