Answer the question
In order to leave comments, you need to log in
How to fix a bug in C++ code?
Please help me fix the bug in the code.
Error code in VS:
Error C3861 gets: identifier not found
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
using namespace std;
int main()
{
char fileName[30], ch;
fstream fps, fpt;
cout << "Enter the Name of File: ";
gets(fileName);
fps.open(fileName, fstream::in);
if (!fps)
{
cout << "\nError Occurred, Opening the Source File (to Read)!";
return 0;
}
fpt.open("tmp.txt", fstream::out);
if (!fpt)
{
cout << "\nError Occurred, Opening/Creating the tmp File!";
return 0;
}
while (fps >> noskipws >> ch)
{
ch = ch + 100;
fpt << ch;
}
fps.close();
fpt.close();
fps.open(fileName, fstream::out);
if (!fps)
{
cout << "\nError Occurred, Opening the Source File (to write)!";
return 0;
}
fpt.open("tmp.txt", fstream::in);
if (!fpt)
{
cout << "\nError Occurred, Opening the tmp File!";
return 0;
}
while (fpt >> noskipws >> ch)
fps << ch;
fps.close();
fpt.close();
cout << "\nFile '" << fileName << "' Encrypted Successfully!";
cout << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
There is no function in the standard library gets
.
You can do the same with this code
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string file_name;
std::cout << "Enter file name: " << std::endl;
std::getline(std::cin, file_name);
std::ifstream ifstream(file_name);
std::string data_to_encrypt;
std::getline(ifstream, data_to_encrypt);
ifstream.close();
for (auto& ch : data_to_encrypt)
{
ch += 100;
}
std::ofstream ofstream(file_name, std::ios::trunc | std::ios::out);
ofstream << data_to_encrypt;
ofstream.close();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question