c++ - 读取 .txt 文件中的特定单词

标签 c++ ifstream turbo-c++

我有一个包含学生姓名和学号的 txt 文件。我想从他的文件中读取并显示特定的卷号。它只显示第一个卷号,但我想读取第二个人的卷号。

也就是如果我要读取“ss”的卷号,它显示的是第一个人的卷号

程序是

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>

void student_read()
{
    clrscr();
    char name[30], n[30], temp[30];
    int i, roll_no, code, count=0;

    ifstream fin("tt.txt",ios::in|ios::beg);
    if(!fin)
    {
        cout << "cannot open for read ";
        return;
    }
cout << "Enter the name of student" << "\n";
        cin >> n;

    while(fin >> name >> roll_no)
    {
        cout << roll_no << endl;
    }



    if(string[name] == string[n])
    {
        cout << "roll no" << "\n" << roll_no;
    }
    else
        cout << "Not found";
}

void main()
{
    clrscr();
    cout << "Students details is" << "\n";
    student_read();
    getch();
}

txt 文件包含以下数据:

苏拉夫 123 SS 33

最佳答案

您的文本文件中是否有每一行的结尾?你有 sourav 123 ss 33 还是 sourav 123\nss 33?还有这个 if(n[30]==name[30])只比较字符串中的 1 个字符。

关于c++ - 读取 .txt 文件中的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510103/

相关文章:

c++ - ifstream 为不存在的文件返回 true

c++ - 尝试添加数组元素给出零

c++ - 如何编译程序以在 64 位机器上运行

c++ - 我应该在头文件中的什么地方声明我的私有(private)类级静态常量?

c++ - 在 Qt HTML5 应用程序中使用 QtWidgets

c++ - 二进制读取器在读取确切字节数时不触发 eof 位

C++ 转换文件流函数以使用字符串流

c++ - 为什么可以从 double 到 float 隐式转换?

c - 如何在 Turbo C++ 中不按 Ctrl+Break 退出无限循环

c++ - 如何在 Google 测试中将 FRIEND_TEST 与 TYPED_TEST 结合使用?