• Register
1 vote
36 views

Problem:

I am a new student of C++ programming. I recently started learning it through YouTube and other free resources. I’ve installed the Visual Studio as my IDE and running my program in it. However, I am trying to read a sample text file in Visual Studio and my written program is supposed to do that. But for some sneaky reasons, it is not happening. Could you please have a look and find out the problem from my codes? Here are the codes I wrote:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main(){
    char mystr[50];
    ifstream f;
    f.open("sample.txt");

    while(f.getline(mystr, 50, '#')){
        cout << mystr << endl;
    }
    f.close();
    return 0;
}

Please, figure out the problem for me and help me to run the code successfully. Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

I’ve given a quick review of your program and pop-up a problem from it. You are supposed to import a text file from your machine and read it through your python program. The problem is, you didn’t specify the path location of your sample.txt file in your program. It means the file (has to be available) is available in your current work directory. But your sample.txt file resides another location or resource directory. So, the solution is pretty much simple. Either put the file in your current working directory or set the file path location in f.open() function to import hat particular file.

Keep asking and learning.

13 9 6
94,260 points

Related questions

1 vote
1 answer 3 views
3 views
Problem: I ahve a visual studio 2010 but I get an error after runing a code: Visual studio unable to start program cannot find the file specified.
asked May 2 PkGuy 23.5k points
0 votes
1 answer 31 views
31 views
Problem : Visual studio system cannot find the file specified.
asked Feb 18 Sabbir Hossain 14.6k points
0 votes
1 answer 9 views
9 views
Problem: I need to solve this matter, please help >Visual c++ redistributable packages for visual studio 2015.
asked May 9 Ashok Dileep 6.7k points
0 votes
1 answer 10 views
10 views
Problem: I'm having an issue with running the latest VC 2017 runtime install (vc_redist.x86.exe) on Windows 10. Previous versions didn't have an issue, but the latest (14.12.25810) seems to. Here's the problem: I'm launching the vc_redist.x86.exe install from another ... previous versions of the VC runtime (e.g. 2015) didn't have this issue. Has anyone else run across this and have a solution?
asked May 1 anika11 32.2k points
0 votes
1 answer 4 views
4 views
Problem: When I define a string in the header file and add an argument to the Player function in the header file everything works fine. But when I add the same argument to the Player function in the cpp file. I get an error: identifier "string" is undefined and I ... including string in the cpp file in hopes of solving the problem but it did not work. I'm really desperate for a solution, guys.
asked Apr 22 anika11 32.2k points
0 votes
1 answer 4 views
4 views
Problem: Hi, any idea that what should I do to fix it .. visual studio c++ console application
asked Apr 3 Ifra 37.2k points
0 votes
1 answer 3 views
3 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 24 rubelkhan 20.4k points
0 votes
1 answer 7 views
7 views
Problem: Hello! I have seen this error many times at the beginning of my programming that says Microsoft Visual C++ 14.0 is required. I always ran to my seniors to get this issue resolved but I could never understand how they resolve it. Can anyone please tell me how we resolve this issue? Thanks!
asked Mar 22 Code Learner 9.9k points
0 votes
1 answer 14 views
14 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 22 sadi1982 36.3k points
0 votes
1 answer 11 views
11 views
Problem: I tried to find out the solution on the web when I face this problem but I did not get any fruitful information that I can continue if you read the above question and understand what is the solution of this please cooperate with me- Thanks
asked Mar 21 shamimkhan 15.1k points