Is multiple times overwriting a file, safe with fstream [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Will fstream overwriting a file multiple times cause any problems?

No, it will not. Try this, you won't get any problems.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream out;
for(int cnt = 0; cnt < 100; ++cnt){
out.open("file.txt");
out << "This will be written 100 times, and erased 99 times.";
// Uncomment the lines below if you want to see each change
//out << " Run#: " << cnt;
//cin.get();
out.close();
}
return 0;
}
Disclaimer: I did not try to run this code. Apologies if there are any syntax errors.

Related

How can we insert a character to a string in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
INPUT STRING
ABCE
INPUT CHAR
D
OUTPUT STRING
ABCDE
It should run for all sizes , and it should be a standard procedure ie run for all the cases.
Can anyone Help me with this? Any help would be appreciated.
You should try the std::string::insert(..). Check out the documentation
#include <iostream>
#include <string>
int main() {
std::string str("ABCE");
std::cout << str << std::endl; // ABCE
str.insert(3, 1, 'D');
std::cout << str << std::endl; // ABCDE
return -1;
}

Find index of symbol in string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.

how to store a quiz score in a txt file using c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to programming and one of the projects I would like to tackle would be a quiz, the problem is I do not know how to store my quiz scores in a text file, can someone help plz? A given example would be nice
Here is slightly modified example of the one given in cplusplus
// basic file operations
#include <iostream>
#include <fstream>
int main () {
std::ofstream myfile("example.txt");
if(myfile.is_open())
{
myfile << "Writing this to a file.\n";
myfile << 1 << 2 << 3;
}
else
{
std::cout << "error opening file" << std::endl;
}
return 0;
}
This creates example.txt in your working directory if it does not exist yet, opens it, streams to it and closes the file as myfile is destroyed. Now the file contains the following text:
Writing this to a file.
123

string sort in c++ nor sorted properly at 5th element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
please find below program, the sort output is not proper.
string pru[] = { "ruthvi$p", "uthvi$pr", "thvi$pru", "hvi$prut", "vi$pruth", "i$pruthv", "$pruthvi", "pruthvi$" };
sort(pru, pru + 8, cmp);
for(int i = 0; i < 8; i++)
cout << pru[i] << " ";
output is
$pruthvi hvi$prut i$pruthv pruthvi$ thvi$pru ruthvi$p uthvi$pr vi$pruth
mistake "thvi$pru" is before "ruthvi$p"
It is not clear how cmp is defined but you can use this code and you will get the expected result.:)
#include <string>
#include <iterator>
#include <algorithm>
//...
std::string pru[] =
{
"ruthvi$p",
"uthvi$pr",
"thvi$pru",
"hvi$prut",
"vi$pruth",
"i$pruthv",
"$pruthvi",
"pruthvi$"
};
std::sort(std::begin(pru), std::end(pru));
for (const auto &s : pru) std::cout << s << std::endl;
The output is
$pruthvi
hvi$prut
i$pruthv
pruthvi$
ruthvi$p
thvi$pru
uthvi$pr
vi$pruth

how can i read a series of character value as character and integer simultaneously and can compare it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a file containing a series of characters like:
......//////////0000000111111111222222222aaaaaaaaaaccccccccccccclllllllllllllllll
I have to scan it from one by one character and have to compare if it is a number or not but in the form of integer.
I used like this:
int x=0;
fscanf(fp,"%d",&x)
if (x>=0 && x<=9)
I must have to read the numbers in the file in integer form and have to compare it.
In c++:
#include <iostream>
#include <locale>
char c;
int i;
while(std::cin >> c) {
if(isdigit(c)) {
i = c - '0';
} else {
//TODO:
}
}
A C++ answer:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream in_file("chars.txt");
char c;
while (in_file >> c)
if (isdigit(c))
cout << c << endl;
}