1

Whenever I try to make a program based on files, my program is never able to open the file itself.

It always ends up executing the part of the program in the "else" part. Is it because my file might not be in the same directory? If so, how do I find the location of my file? Here's the code. I just wanted to check if its working fine or not by inputting and outputting the strings using the concept of files.

int main()
{
char str1[20], str2[20];
FILE *pFile; 
pFile = fopen("Rocket.txt", "r+"); 

if (pFile != NULL) {
    while (feof(pFile)) {
        cout << "Enter String 1: " << endl; 
        fgets(str1, 20, pFile);  
        cout << "Enter String 2: " << endl; 
        fgets(str2, 20, pFile); 

        cout << "The Strings input are: " << endl; 
        fputs(str1, pFile); 
        fputs(str2, pFile); 

    }

}
else {
    cout << "File not opened." << endl; 
}
Jakub Muda
  • 6,008
  • 10
  • 37
  • 56
  • 3
    Please read https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Ed Heal Apr 14 '18 at 05:24
  • 1
    And why are you not using `fstream`? - stdio is C – Ed Heal Apr 14 '18 at 05:25
  • What programming tool (editor, compiler, IDE?) do you use? Where in the file system is your file? Where your executable? How do you run the program? What's the current directory when the program runs? (I'm asking this because when you use Visual Studio the current directory will be something unexpected, and my guess is that Rocket.txt is not there. Hint: Use an absolute path like "C:\\Users\\Lakshya\\Rocket.txt"); also choose a simple directory path at first.) – Peter - Reinstate Monica Apr 14 '18 at 05:28
  • The IDE: Visual Studio 2017 – Lakshya Gupta Apr 14 '18 at 05:30
  • You can find out your program's notion of its current directory with the aptly named Windows function `GetCurrentDirectory`. An example on how to use it is at https://msdn.microsoft.com/en-us/library ./aa363806(v=vs.85).aspx – Peter - Reinstate Monica Apr 14 '18 at 05:34
  • Another remark is that (1) you want to test for the *absence* of end-of file, i.e. you must test for `!feof()`. (2) Also heed @EdHeal's comment: The test must be *after* you attempted to read, but *before* you use the read data (because only the read attempt detects the end-of-file condition, and in that case no data was read. For example, after opening an empty file `feof()` would be false, until you try to read something, which immediately encounters the end of the file.) – Peter - Reinstate Monica Apr 14 '18 at 05:38
  • `while(feof(pFile))` means the inner loop will never be executed. You probably intend to do `while(!feof(pFile))` (note the additional `!`). That qualifies as a typo, so I'll vote to close accordingly. Note that a `while(!feof(pFile))` loop is also a bad idea in practice - read https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong for more information. – Peter Apr 14 '18 at 05:47

1 Answers1

1

1) "How do I find the location of my file?"

  • right-click on your file
  • properties->Details->Folder path or
  • properties->General->Location

For example if it was C:\Users\user\Desktop then the location you write is c:\\Users\\user\\Desktop\\Rocket.txt

2) In your code, I found two bugs

Notice that in the "while condition" you should write (feof(pFile)==0) or (!feof(pFile)) because it should continue reading from file until it reaches the end of file and feof(pFile) == 1.

"puts" writes at the end of the file. So when the file marker is not at the end of the file, it doesn't write on it, unless your initial file has just 37 characters. So it's not a good idea to use "puts" in this program. Instead , you can use cout to check out values of str1 and str2.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Sara98
  • 13
  • 4