strange thing in sequential file access - c++

Recently, I am trying to write codes to get trained in sequential file access. I learned it well, but the issue is kinda stressing me out. I have a code that work 100%, and its task is "make a code that prints array elements inside a file", the text file name is "numric".
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int a[3]={5000,6000,7000};
ofstream outfile("numric.txt");
if (outfile.is_open()){
outfile << "employee payroll:" << endl;
for (int i=0;i<3;i++)
outfile << a[i] << endl;
outfile.close();
} else
cout << "failed!" << endl;
return 0;
}
I implemented the code in two different program (VS Code, dev++) and it works fine. It found the file, but when I open the text file, there isn't any text inside it. The code should do its work by finding some text inside it if I opened it.
Note:sometimes when the code works, the antivirus pops a message saying it found an item that doesn't look safe in the program and deletes it.

Related

How can I test if either of two files exist?

I am new to C++,
I am using a while true to find a Pass.txt file.
Once it finds it the while loop breaks and prints "Pass.txt file FOUND"
This works fine
I then would like to see if it can find Pass.txt OR Fail.txt with a while true
something like this ifstream ifile("Pass.txt || Fail.txt");
This does not work:
cout << " ------------------------------------------\n";
cout << " Searching for Pass or Fail file \n";
cout << " ------------------------------------------\n";
cout << "\n\n";
while (true)
{
ifstream ifile("Pass.txt");
//ifstream ifile("Pass.txt ||Fail.txt");
if (ifile)
{
// The file exists, and is open for input
break;
}
}
cout << "\n\n";
cout << " ------------------------------------------\n";
cout << " Pass.txt FOUND \n";
cout << " ------------------------------------------\n";
cout << " ------------------------------------------\n";
Your code fails because you’re not testing for the existence of two files. You are testing whether the one file with the weird name Pass.txt || Fail.txt exists. You need two make two separate existence checks, one for each separate file.
To do this, don’t open the file — use std::filesystem::exists:
#include <filesystem>
#include <thread>
// …
int main() {
using namespace std::chrono_literals;
namespace fs = std::filesystem;
while (not fs::exists("Pass.txt")) {
std::this_thread::sleep_for(500ms); // Be gentle to the system.
}
while (not (fs::exists("Pass.txt") or fs::exists("Fail.txt"))) {
std::this_thread::sleep_for(500ms); // Be gentle to the system.
}
}
(You can also write ! instead of not, and || instead of or; but I prefer using these descriptive terms in my own code.)
We use std::this_thread::sleep to avoid hammering the system with a busy loop, which will spin up your CPU like crazy even though your code isn’t doing any real work.
However, note that merely testing for a file’s existence isn’t very useful. And your second loop will immediately terminate, since "Pass.txt" exists after the first loop (unless, by a freak coincidence, it was deleted in the nanoseconds between the two loops).

how i can delete a file in c++ in visual studio 2019 using gets or without using it

I want to be able to delete a file using c++ remove functions but gets does not work in visual studio 2019 and using fget and gets_s ,the program runs but everytime it shows message that file not deleted as i proggrammed, The question is when it will delete .I tried every thing but it doesn't work.
There would be solutions but i am a student so plz consider my problem.Here is my code :
#include <iostream>
#include <conio.h>
#include <stdio.h>
int main()
{
int status;
char file_name[59];
cout << "enter name of file to delete:\n ";
gets(file_name);
status = remove(file_name);
if (remove(file_name) == 0) //i wrote if(file_name==0); too but it doesn't work
cout << "file deleted";
else
cout << "file not deleted";
}
I was looking for answers in stack over flow other similar questions but they were too professional to be understand by me plz tell me simple way to delete a file that user wants to delete.

Visual Studio 2012. "unable to start program" *.exe "cannot find the file specified"

Yes, I know this is a common question. No other question/answer sets meet my needs here. Taking c++ this quarter. Been going along just fine. Trying to do last assignment, and I'm getting the above error, even on a fresh project with:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
As an experiment, I tried pasting in much more elaborate code from previous assignments. This runs. Just pasting in -- not running the previous assignment.
I took the simplest of them, and stripped it down to this:
Main.cpp:
#include <iostream>
using namespace std;
int main()
{
double hours, rate;
cout << "Hello World";
cin >> hours;
cout << "Hello World";
cin >> rate;
system("pause");
return 0;
}
This compiles and runs fine. However, if I remove either of the cin lines I get the error stated in the title.
I'm just flabbergasted because pasted in code compiles and runs, but even "Hello World" in a fresh project won't.
Does my above example provide any clues?
Does it happen if you run it as administrator? Were you running it from Visual Studio or did you clicked the executable?
If you have an anti virus, it may be causing the error as well.

Reading data from a .csv and into arrays in c++

I did some searching on this site and on google as well. But i couldnt understand a lot of code i seen and im hoping to find more direct help from here. Im only 2 semesters in for c++ and i have a side project id like to do for my boss.
He generates a csv file for call logs and i want to be able to retrieve certain lines from the log and be able to calculate and display data.
Im not sure of the exact questions i need to ask but heres my code where i tried to start getting data but ran into problems (my programming knowledge is fairly limited due to lack of time and experience :)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
//opens the csv file.
ifstream gwfile;
gwfile.open("log.csv");
if(!gwfile) { // file couldn't be opened
cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
cin.get();
return 0;
}else
cout << "SUCCESSFULLY opened file!\n";
cout << "-------------------------------------------------------------------------------\n\n\n";
long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;
//in the csv im trying to only read lines that start with the voice as those are only valid data we need.
//also i would like to display the headings in teh very first line
while( !gwfile.eof() ){
//This is where im trying to only accept the lines starting with "Voice"
//if(data[SIZE] == "Voice"){
for( int i=0; i!=","; i++){
cout << "This is i: " << i << endl; //testing purposes.
}
//}
// getline(gwfile, data, '');
// cout << data[0];
}
return 0;
}
Let’s begin with the obvious
long int SIZE = 0;
char data[SIZE];
cout << "This is data SIZE:" << data[SIZE] << endl;
You are creating an array of size 0, then reaching for its first member: data[0]. This cannot work. Give your array a size that is large enough to handle the data you want to treat, or use a dynamicly resizable container (such as std::vector) to deal with it.

Stroustrup swan book Vector Problem

I'm using Stroustrup's swan book. I have run into a problem getting output from a
vector. I followed the text example from sec. 4.6.3 on page 121. I
managed to get the source compiled and am able to execute it. After
typing in a list of whitespace separated words, the program hangs and
does not list the elements of the vector as it should. I realize not
every element will be outputted if it is repeated, but i receive no
output at all. I have compiled and run this using the g++ 4.3.2
compiler on Linux and using the Visual C++ express 2008 compiler on
windows. Both produce the same result. Thank you for taking time to
read this. Here is my source:
#include "Supporting_files/std_lib_facilities.h"
int main()
{
vector<string> words;
string temp;
cout << "Enter a list of words: ";
while(cin>>temp)
words.push_back(temp);
cout << "Number of words: " << words.size() << endl;
sort(words.begin(),words.end());
for(int i=0;i<words.size();++i)
if(i==0||words[i-1]!=words[i])
cout << words[i] << "\n";
}
while(cin>>temp) only ends when it hits an end of file. Use control-D to send an end of file into the terminal.