Saving many images with its dynamic name [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I need to save images captured from camera just like
"D:\storage\img1" then I press "s" another time and program should save
"D:\storage\img2" and then
"D:\storage\img3"
so everytime I press a custom key it will save an image with different name.
How to do that?
Thanks for all your responses

This code concatenates (adds) the value of int i to string filename. This is done through IntToStr() . And int i loops from 0 to 20 and thus creating your "dynamic name".
File output :
PhotoImage0.txt
PhotoImage1.txt
PhotoImage2.txt
..
..
PhotoImage19.txt
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string IntToStr(int n)
{
stringstream result;
result << n;
return result.str();
}
int main ()
{
ofstream PhotoImageFile;
int Number_of_files=20;
string filename;
for (int i=0;i<Number_of_files;i++)
{
filename="c:\\PhotoImage" + IntToStr(i) +".txt";
cout<< filename << " \n";
PhotoImageFile.open(filename.c_str());
PhotoImageFile << filename<<" : Writing this to a file.\n";
PhotoImageFile.close();
}
return 0;
}

On startup, iterate the folder with an 'img*.*' mask - how you do this is up to your file system API. Use string functions or a loop to extract the part of the filename representing the number and convert it to an int. Every time you need to save a file, add 1 to the int, convert back to number-string & then concat the path, "img", number-string and extension to assemble the new file spec.

Related

Need to set a array string length so that it is exactly 13 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
hi there i'm currently finishing a project at college but need to set a length of 13 to a string so that it will return an error message if it is shorter or longer than 13 my code so far is
void add_new_text_book()
{
//this option is here to add a new book to the list
printf("Please enter the Title of a new text book\n");
scanf("%s",book[number_of_books].title);
printf("Please enter the Authors firstname\n");
scanf("%s",book[number_of_books].firstname);
printf("Please enter the Author surname\n");
scanf("%s",book[number_of_books].surname);
printf("Finally please enter the ISBN number of the book\n");
scanf("%s",book[number_of_books].isbn);
if(length_of_string==13)//will be used to check the length of the book is valid
{
if(number_of_books==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}else{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
length_of_string=strlen(book[number_of_books].isbn);
but even when i enter 13 it comes up with the error message it only seems to accept 123-456-789-1 any help will be greatly accepted
It would help considerably if you calculated length_of_string before you used it, rather than after.
You can use str.size() to do your calculations.
Taken from C++ reference
// string::size
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.size() << " characters.\n";
return 0;
}
what the value of "length_of_string" ?
is its value is :123-456-789-1?
if so you can use it :
if(length_of_string.size()==13)//will be used to check the length of the book is valid
{
if(number_of_books.size()==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}
else
{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
}

Reading string from vector until whitespace in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is probably easy but im not sure how. I tried searching multiple websites and yes Google and couldn't find anything on this.
My vector result[0] looks like this
A3 * * B4 * *
Declaration
vector<string> result = v.formVectorFile("Prj3 Config.txt");
I know that cin reads until whitespace so I was trying to use this to figure it out.
If I read straight from fstream I can read until whitespace, but im trying to do this with a string inside a vector and something like result[0] >> s; obviously doesnt work.
I need to read until it hits a whitespace then read the next one until whitespace. Etc...
So extract A3 by itself. Operate on it then extract * etc...
Your question is unclear because you don't tell us precisely what result is.
If we can assume that result is a std::vector<std::string>, then you can do something like this:
std::istringstream iss(result[0]); // consider only first string in vector
std::string item;
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}
If we assume that result is std::vector<char>, then you can do this:
std::string s(result.begin(), result.end()); // consider entire vector as single string
std::istringstream iss(s);
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}

Saving results to a file in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem with this code.
What I am looking for in the code is to get the result of "first" and "second" randomly and put the result in a file.
It works great if I run it without using the file and I get all the correct results, but when I try to save the result in the file, I get only the first node which contains (first, secnd).
Here is the code:
#include<iostream>
#include <fstream>
#include<cmath>
using namespace std;
void main()
{
int first[100],secnd[100];
for (int i=0; i<100 ;i++)
{
first[i]=rand()%500; //random number from to 499
secnd[i]=rand()%500; //random number from to 499
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile <<first[i]<<" "<<secnd[i];
myfile.close();
}
}
You are opening your file in the wrong place. You open the file once before the loop and close the file once after the loop. Like this.
myfile.open ("example.txt");
for (int i=0; i<100 ;i++)
{
...
}
myfile.close();
When you open a file for output you delete what is currently in the file. So if you open the file inside the loop then you are losing the random numbers you had written before.
The default open-flags for an ofstream are to truncate (overwrite) the file. You are opening and closing the file every time through the loop. If you want all results in the file, then open it ONCE outside the loop, and close it after the loop.
You're reopening the file in the loop. This zeros the file each time, so you only get the last entry.

characters and files in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to make a program where the filename is composed of two parts: the first one is fixed and the second one can change during the program e.g "fixpart_integer.dat". I tried to do this in C++ but I did not succeed. The fisrt probelm is: how can i convert a number to a char ; and how can i concanate these 2 characters ; and how to declare this final char in the right way in order to open this filename ?
Many questions but I did find an easy way to do this.
#include <string>
#include <sstream>
std::string make_filename(std::string prefix, int id) {
std::stringstream ss;
ss << prefix << "_" << id << ".dat";
return ss.str();
}
and then I convert the string to char.
if you are working with c you may try the function described in here itoa
else if you are working woth c++ you may try something like this:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

how would one go about finding specific words in a text file in c++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
how would i do this:
Got a text file called directorycontents.txt in this directorycontents.txt there is a bunch of text each one is a filename with a filename extension i want to be able to go like this if there is a filename extension of specific characters like .txt or .png then do fprintf(stderr,"whateva");
i have looked at istream and fstream and iostream but im not really shore how to use fstream to do this
thanks
Okay, I'll just point you to the right direction and I won't post any code, as you need to try it by yourself.
First of all, read about reading files in C++. You can google it and there are tons of information about this. You can try with "how to read text file in C++", for example.
Second, prefer using ofstream and/or ifstream - this is the C++ way to do it.
Then parse the file - you can read it word by word (using istream::operator>> ) , line by line (for example with getline ) into std::string (as you're talking about file names).
And then analize the input - analize the parsed file and search for specific words in it - for example, std::string has member functions like find - I think this will be enough for your problem :)
I hope that helps. Just note, that we don't write code here, we just help finding solutions for problems.
For something like this definitely take a look at std::fstreams. Based on your vague description of what you're trying to do, you can use this simple program as a starting point:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void doSomething();
int main(int argc, char *argv[])
{
if(argc < 2)
{
cout << "Usage: findsomething [filename]" << endl;
return 1;
}
ifstream infile(argv[1], ifstream::in);
if(!infile.is_open())
{
cout << "Couldn't open file " << argv[1] << endl;
return 1;
}
string line;
while(getline(infile, line))
{
if(line.find(".txt") != string::npos ||
line.find(".png") != string::npos ||
line.find(".bat") != string::npos)
{
doSomething();
}
}
}
Hopefully, that's enough code to get you started and it isn't too difficult for you to read.