Вот пример текстового файла, который я имею дело с:Как скопировать строку в txt-файл?
http://example.com/object1 50 0
http://example.com/object2 25 1
http://example.another.com/repo/objects/1 250 0
ftp://ftpserver.abc.edu:8080 13 5
...
Я хочу передать URL, размер (первый номер), а также приоритет (второй номер) в массив. Вот мой код:
ifstream infile;
infile.open("ece150-proj2-input.txt");
//Get the number of lines in the text file
int lines = 0;
while (!infile.eof()) {
string line;
getline(infile, line);
lines++;
}
//Get the components in the input file
char url[lines][50];
float size[lines];
float delay[lines];
for (int i = 0; i < lines; i++) {
infile >> url[i] >> size[i] >> delay[i];
}
//Testing if I get the url address correctly
cout << url[0] << endl;
cout << url[1] << endl;
Однако результат несколько странный характер:
pĞQ?
?Q?
Почему это происходит? Может ли кто-нибудь решить эту проблему. Спасибо ;-)
Вы уже прочитали EOF после цикла while, так что читать нечего. – Doorknob