从文件获取输入 [英] Getting input from file

查看:79
本文介绍了从文件获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我一直在做一个项目,我从一开始就遇到了问题。我很感激任何帮助。



所以在这个任务中,我应该从文件中获取输入,从我学到的知识中我可以使用ifstream。我写了一个条件,如果文件没有打开发送给我一个错误消息。

我的问题是,它不起作用,我一直收到错误信息,它似乎可以'打开文件,我找不到为什么它不会打开文件。

 /////////////////////////我的头文件
#ifndef header_h
#define header_h


#include< iostream>
#include< fstream>
#include< vector>
#include< string>使用namespace std

;

struct cam {
string name;
string post;
int age;
int salary;
};

void data_input(string fn1,vector< cam>& W); / *从我们的输入文件中输入数据* /
#endif
/////////////////////////


/////////////////////////主
#include< iostream>
#include< fstream>
#includeheader.h
using namespace std;

string inputfile =input.txt;
string outfile =output.txt;
vector< cam> employee_details;

int main()
{
data_input(inputfile,employee_details);
返回0;
}
/////////////////////////


//// /////////////////////这个cpp文件
#include< stdio.h>
#include< iostream>
#include< vector>
#include< fstream>
#includeheader.h

using namespace std;

void data_input(string fn1,vector< cam>& W){
string tmp;
ifstream f1(fn1.c_str());

if(f1.is_open())
{
while(f1.good()))
{
//读取名字
getline(f1,tmp,';');
cam tmp_cam;
tmp_cam.name = tmp;
W.push_back(tmp_cam);

//读取年龄
getline(f1,tmp,';');
W.back()。age = atoi(tmp.c_str());

//读取等级
getline(f1,tmp,';');
W.back()。post = tmp;

//读取工资
getline(f1,tmp,';');
W.back()。salary = atoi(tmp.c_str());

}

f1.close();
}
其他
{
cout<< 打开文件时出错<< ENDL;
}

}
/////////////////////////





我尝试了什么:



我能找到的所有东西网,但我觉得我错过了一些非常小的东西,这就是为什么我想到为什么不在这里分享它并从不同的视图中看到它

解决方案

你可以打印打开失败时出现系统错误消息:

 ifstream f1(fn1.c_str()); 
if (f.fail())
cerr<< 无法打开文件:<< strerror(errno);

正如Richard已经指出的那样,当前目录中可能不存在该文件。然后你应该移动或复制它或使用文件的绝对路径。


看起来你的应用程序找不到文件。最好说文件不是正在运行的进程的目录。这可能有所不同,所以最好看看项目的设置。



一如既往最好使用完整路径文件操作。因此输出过程也是如此!!!



学习使用调试器。这个调试器教程提供了一些基本知识。

Hi guys, I've been working on a project and I'm having problems with the very beginning of it. I would appreciate any help.

So in this task, I should get my input from a file and from what I've learned I can do that using ifstream. I wrote a condition that if the file didn't open send me an error message.
and my problem is, it doesn't work and I keep getting the error message and it seems like it can't open the file and I can't find out why it won't open the file.

///////////////////////// my header file
#ifndef header_h
#define header_h


#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct cam{
    string  name;
    string post;
    int age;
    int salary;
};

void data_input(string fn1, vector<cam> & W);         /*input data from our input file*/
#endif
/////////////////////////


/////////////////////////the main
#include <iostream>
#include <fstream>
#include "header.h"
using namespace std;

string inputfile="input.txt";
string outfile="output.txt";
vector<cam> employee_details;

int main()
{
    data_input(inputfile, employee_details);
    return 0;
}
/////////////////////////


/////////////////////////this the cpp file 
#include <stdio.h>
#include <iostream>
#include <vector>
#include <fstream>
#include "header.h"

using namespace std;

void data_input(string fn1, vector<cam> &W){
    string tmp;
    ifstream f1( fn1.c_str() );
   
    if( f1.is_open() )
    {
        while( f1.good() ))
        {
            //reading names
            getline (f1,tmp,';');
            cam tmp_cam;
            tmp_cam.name=tmp;
            W.push_back(tmp_cam);
            
            //reading the ages
            getline(f1,tmp,';');
            W.back().age = atoi( tmp.c_str() );
            
            //reading the ranks
            getline (f1,tmp,';');
            W.back().post = tmp;
            
            //reading the salaries
            getline (f1,tmp,';');
            W.back().salary = atoi( tmp.c_str() );
            
        }
      
        f1.close();
    }
    else
    {
        cout << "Error to open the file" <<  endl;
    }
    
}
/////////////////////////



What I have tried:

every thing I could find on the net but I feel I'm missing something really small that's why it came to my mind why not sharing it here and see it from different views

解决方案

You can print a system error message when opening fails:

ifstream f1( fn1.c_str() );
if ( f.fail() )
    cerr << "Failed to open file: " << strerror(errno);

As already noted by Richard the file may not exist in the current directory. Then you should move or copy it or use absolute paths for your files.


Looks like your app isnt finding the file. Better to say that the file isnt the directory of the running process. This could differ, so better take a look in the setings of the project.

As always it is best to use the complete path for file operations. So also for the output process!!!

Learn to use the debugger. This debugger tutorial is providing some basic knowledge.


这篇关于从文件获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆