Home > Blockchain >  Running a C Script In Visual Studio 2022 keeps deleting text within a file, not sure what's w
Running a C Script In Visual Studio 2022 keeps deleting text within a file, not sure what's w

Time:10-28

I've been stuck working on this painful program for a few hours now in Visual Studio 2022, and am struggling to wrap my head around why it keeps deleting text in a different file each time I run it.

For reference: For a C related assignment I have, I have to design a program that opens a file (age.txt in this instance), and displays the data within it (which should be 25).

Here's the code I have written, for reference:


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

using namespace std; 

int main() 
{
    ifstream din;
    ofstream dout;
    string inpFileName;
    string age; // The user's age

    // Prompt for the age:
    cout << "Please provide the file containing your age" << endl << endl; 
    cin >> inpFileName;

    din.open(inpFileName.c_str());      // File extension is arbitrary
    dout.open("age.txt");   // 

    if (din)
    {
        // Read the age:
        getline(din, age);
        din >> age;

        // Output the age
        cout << "Your age is: " << age << " years." << endl;

    }
    else
        cout << inpFileName << " was not found. " << endl;

    din.close();
    dout.close();

    system("pause");    

    return 0;
}

And oddly enough, it seems to work fine for the most part, as it prompts me to enter a file, where I insert age.txt. However, each time I try to insert the file in there, this is the output I get:

Please provide the file containing your age

age.txt Your age is: years. Press any key to continue . . .

Where it appears that, each time I run the program, it deletes the number "25" from age.txt, and proceeds to give me a blank result instead.

Would anybody know what's causing this, and if possible, what's a solution that I could use to stop my C program from deleting the text in my attached file?

Here, some things I've tried to do in order to fix the problem has included:

-Reworking if/else statements (didn't work) -Reworking cout/dout statements (didn't work) -Removing and adjusting file lines in both my script and in age.txt (didn't work) -Adjusting the properties of the strings (didn't seem to work? Didn't get the intended output). -Playing with #include statements whilst trying to get results (No luck there either) -Adjusting administrative settings on my computer to see if it was blocking anything (no luck)

Among other attempted fixes, with no luck whatsoever. I feel completely lost on this. (For reference, I'm using Visual Studio 2022 for this script)

CodePudding user response:

First, a mini-code review:

#include<iostream>  // Poor spacing
#include<string>
#include<fstream>

using namespace std;  // Bad practice

int main() // I'm noticing that a lot of your lines all have an extra space at the end
{
    ifstream din;  // Don't front-load your declarations; declare when you need them.
    ofstream dout;  // As described, you don't need this at all.
    string inpFileName;
    string age; // The user's age  // Isn't an age an integer?

    // Prompt for the age:
    cout << "Please provide the file containing your age" << endl << endl;
    cin >> inpFileName;

    // Conversion to a C-string has not been necessary for about a decade.
    din.open(inpFileName.c_str());      // File extension is arbitrary
    dout.open("age.txt");   //

    // Better to check for the error up front.
    if (din)
    {
        // Read the age:
        getline(din, age);
        din >> age;

        // Output the age
        cout << "Your age is: " << age << " years." << endl;

    }
    else  // Poor formatting
        cout << inpFileName << " was not found. " << endl;

    din.close();
    dout.close();

    system("pause");    // Lots of garbage whitespace on this line.

    return 0;
}

The biggest issue is the extra output file stream. By default, an output file stream wipes the file it opens. You don't even need one, based on the description provided. Getting rid of it would be the minimum thing needed to fix your code.

And, while I fully understand this will likely be beyond what you've learned, passing the file name as an argument to main() would be far better.

#include <fstream>
#include <iostream>

int main(int argc, char** argv) {
  if (argc != 2) {
    std::cerr << "Need a file name.\n";
    return 1;
  }

  std::ifstream din(argv[1]);
  if (!din) {
    std::cerr << "Error opening file.\n";
    return 2;
  }

  int age;
  din >> age;
  std::cout << "Your age is: " << age << '\n';
  din.close();  // Technically not necessary; the OS will close the file since
                // the program is ending anyway, but consider it a best
                // practice to handle your business yourself. You'll learn
                // ways to automate this later on.
}

Output:

❯ ./a.out age.txt
Your age is: 25
  • Related