即使在指定完整路径后也无法在C ++中打开文件

时间:2016-03-29 04:09:40

标签: c++

我的下一步是尝试getcwd(),但我想知道我的代码中是否有错误,我没有看到。 student_status.txt位于正确的文件夹中,我尝试了完整的文件路径。

此外,代码会编译并通知我该文件未打开。

有什么想法吗?

 using namespace tenPoint;
 int main()
 {
    string name;
    char grade;
    double average;

    cout << "test";

    ifstream fileRead;
    fileRead.open("student_status.txt");
    if (fileRead.is_open())
    {
        while (!fileRead.eof())
        {
            getline(fileRead, name);
            fileRead >> average;
            fileRead.ignore();

            switch (finalGrade)
            {
            case F:
                grade = 'F';
            case D:
                grade = 'D';
            case C:
                grade = 'C';
            case B:
                grade = 'B';
            case A:
                grade = 'A';
            }

            cout << name << " " << average << " " << finalGrade << endl;
        }
        fileRead.close();
    }
    else
        cout << "file wasn't open";
 }

1 个答案:

答案 0 :(得分:1)

我必须在finalGrade循环中注释掉所有内容(std未定义),将名称空间更改为#include并为iostream添加fstream行和 fileRead.open("student_status.txt"); 以使程序编译。将源文件放在我的桌面上,将g ++可执行文件输出到我的桌面,在桌面上放置一个名为“student_status.txt”的文件。该程序运行正常,它打开文件进行阅读。我不需要任何绝对的路径。我在OS X上。

首先确保您的文件名正确,符号,大小写等等,并且它与 / executable / 输出文件位于同一目录中。也许尝试替换

 fileRead.open("./student_status.txt");

./

注意 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <link rel="Stylesheet" type="text/css" href="css/smoothTouchScroll.css" /> <link rel="stylesheet" href="dist/jquery.rondell.css" type="text/css" media="all" title="Screen"> <script type="text/javascript" src="libs/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="libs/modernizr-2.0.6.min.js"> </script> <script type="text/javascript" src="dist/jquery.rondell.js"></script> <link rel="Stylesheet" type="text/css" href="css/smoothTouchScroll.css" /> <link rel="Stylesheet" type="text/css" href="css/style1.css" /> <style type="text/css"> #TouchScroller { position:relative; width:auto; height:auto; } #TouchScroller div.scrollableArea img { position: relative; float: left; padding: 0; margin: 0; width:100%; height:50%; } </style> <script src="js/jquery-ui-1.10.3.custom.min.js"></script> <script src="js/jquery.kinetic.min.js"></script> <script src="js/jquery.smoothTouchScroll.min.js"></script> </head> <body> <div> <sql:setDataSource var="webappDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://pshrivas-eni.cwgbkmxxzso1.ap-southeast-1.rds.amazonaws.com:3306/eni_support" user="db_user" password="password123" /> <sql:query dataSource="${webappDataSource}" sql="select imgid,count1 from carousel_three" var="result1" /> <div id="car1" class="carousel"> <div id="rondellCarousel" class="hidden"> <c:forEach var="row" items="${result1.rows}"> <img id="img" src="${pageContext.servletContext.contextPath }/Imagedb?id=${row.imgid}" /> </c:forEach> </div> </div> </div> <script type="text/javascript"> $(function() { // Create a rondell with the 'carousel' preset and set an option // to disable the rondell while the lightbox is displayed $("#rondellCarousel").rondell({ preset: "carousel", onAfterShift:function(idx){ // alert(idx); itemLoadCallback: trigger(idx) } }); }); function trigger(idx, state) { //document.getElementById('TouchScroller').innerHTML=" "; $.ajax({ type : "GET", url : "Sampleimage1", contentType :"application/json", data:{id:idx}, success : function(data) { $.each(data,function(index,item){ $.each(item,function(i,obj){ var img = $('<img id="dynamic">'); //Equivalent: $(document.createElement('img')) img.attr('src','data:image/jpg;base64,' +obj.map.key ); img.appendTo('.scrollableArea'); // $('.scrollableArea').append('<img src="data:image/jpg;base64,' +obj.map.key+'" />'); }); }); } }); } </script> <div id ="car2" class="carousel1"> <div id="TouchScroller"> </div> </div> <script> $("#TouchScroller").smoothTouchScroll({ continuousScrolling: true }); </script> </body> </html> 告诉程序查看当前目录。

相关问题