-
Unsized string input
Hi (btw, I'm new in this Forum)
I'm doing a project and I have a problem with strings and char arrays (mainly because of the limited size, but also some other stuff).
I'm not sure if I need to put here the code I've done, since it's mainly small stuff, but I'll do it if you find the need.
In the program I need to open a text file chosen by the user, so I put an ifstream. Problem is: I'd like the user to be able to input an unlimited char array by a simple
cout<< "Path to file: ";
cin>> Path;
or something like this...
I tried with strings (getline) and then converting it to a char array (cause ifstream only accepts the path in char array), but for some reason the program stopped once I got to the getline part (I used "getline (cin, Filename)"). I searched for the reason, couldn't find it, tried another way.
Right now I just have:
char Path[200]
cout<< "Path to file: ";
cin>> Path;
but it will only read until it finds a " " (if I'm not wrong).
Later, there's another problem, but it's similar so, once I find a solution for that one, the other one is probably solved.
(In the project, I'm going to take the text from the text file, put it to a string and then pass it to a linked list... any suggestions about that would be greatly apreciated.
Also, I'm on my first year with Programming, so please don't get too complicated.)
Thanks in advance.
-
Code:
std::string Filename;
std::getline(std::cin, Filename);
std::istream File(Filename.c_str());
-
That's what I tried, but it skips the input part.
This is what I have.
Sorry, the variables are in portuguese... But you're programmers, I think you'll get it :)
Code:
int Opcao;
do
{
cout<< "Pretende criar um ficheiro novo ou abrir um guardado? (1 - Criar Novo / 2 - Abrir Guardado): ";
cin>> Opcao;
}while(Opcao!=1 && Opcao!=2);
if(Opcao==1)
{
char Texto[2000]; //<------------ I wanted this to be an unlimited char array.... but this will do for now. I'll change it later.
cout<< "Escreva o texto: " <<endl;
cin>> Texto;
}else
{
string Caminho;
cout<< "Escreva o caminho para o ficheiro: ";
getline(cin, Caminho); //<------- It skips this part, so I dont even have a chance to input anything.
ifstream Ficheiro(Caminho.c_str());
if(!Ficheiro.fail())
{
cout<< "Falha ao abrir o ficheiro." <<endl;
cout<< "Verifique se escreveu correctamente o caminho." <<endl;
cout<< "Exemplo: C:\user\folder\texto.txt" <<endl;
}
else
{
while (!Ficheiro.eof())
{
cout<< "ahh!"; //(just for testing)
}
Ficheiro.close();
}
}
So the main problem seems to be on the getline, that just stops/skips... I'm working with VC++ 2008, btw.
-
If you use both cin >> and getline for input, then before the calls to getline you'll need to clear the input buffer. Otherwise it will just read the '\n' left there by cin >> and continue.
See the C++ implementation of myflush here.
-
Also put your code inside [code]Code tags[/code].
Otherwise code will be unreadable.
-
ok, I added a cin.ingore() before the getline and it's now working :)
thanks
EDIT:
just one small thing:
Code:
string Caminho;
cout<< "Escreva o caminho para o ficheiro: ";
cin.ignore();
getline(cin, Caminho);
ifstream Ficheiro(Caminho.c_str());
now I have this thing (rest is still the same)
now I tested it, anything I write as the path (even something like "sjkfbaf") will show as OK... meaning I get a full page of "ahh!"
do you know why this happens? :/
(it was working kinda fine just a while ago, it showed the "error case" and when i wrote the path right, it showed the "fine case")
-
>> If you use both cin >> and getline for input, then before the calls to getline you'll need to clear the input buffer.
I prefer to state this as, if you use both cin >> and getline for input, then after calls to cin >> you'll need to ignore the trailing newline.
The reason is that it is the trailing newline after calls to cin >> that cause the problem. If you call getline twice in a row, then the second call shouldn't have an ignore() before it or it will ignore the first character in the filename. If you always put the cin.ignore() after calls to cin >>, then you don't have that problem (and it is safe to do so even if you never use getline).
>> just one small thing:
I'd expect invalid paths to show the full page of "ahh!" Are you saying it doesn't work when you give it a proper path? Does it work if you hardcode the proper path into the program? (Make sure you use \\ or / instead of \ in your paths inside the code.)
What is the correct path that isn't working?
-
(I was using \\ but didnt know you could use just /)
Ah...
My mistake...
Code:
if(!Ficheiro.fail())
was suposed to be
Code:
if(Ficheiro.fail())
My fault... now it's working properly, thank you all :)
(now I'm gonna bury myself in the rest of the project or suicide in a desperate atempt of avoiding the consequences of not doing it :) )