У меня есть программа в c++, которая используется для чтения некоторого текста из TXT файла с использованием функций fstream. Но на экране вывода он показывает дополнительный вывод цикла while, который нежелателен. Поэтому, если tt.txt содержит данные
ss
123
то выход
Name ss
roll no 123
name
roll 123
Код:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
void student_read()
{
clrscr();
char name[30];
int i,roll_no;
ifstream fin("tt.txt",ios::in,ios::beg);
if(!fin)
{
cout<<"cannot open for read ";
return;
}
while(!fin.eof())
{
fin>>name;
cout<<endl;
fin>>roll_no;
cout<<endl;
cout<<"Name is"<<"\t"<<name<<endl;
cout<<"Roll No is"<<roll_no<< endl;
}
}
void main()
{
clrscr();
cout<<"Students details is"<<"\n";
student_read();
getch();
}
См. FAQ C++ для получения справки по I/O: http://www.parashift.com/C++-faq/input-output.html
#include <iostream>
#include <fstream>
void student_read() {
char name[30];
int roll_no;
std::ifstream fin("tt.txt");
if (!fin) {
std::cout << "cannot open for read ";
return;
}
while(fin >> name >> roll_no) {
std::cout << "Name is\t" << name << std::endl;
std::cout << "Roll No is\t" << roll_no << std::endl;
}
}
int main() {
std::cout << "Students details is\n";
student_read();
}