I'm trying to build a program able to read all the characters including control characters and whitespaces in a jpg file .... i made it read \n or \t but i can't add there both together or any other control character ...
here's my code..
#include <string>
#include <bitset>
#include <iostream>
#include<fstream>
using namespace std;
int main(){
ifstream file("E:\\2.jpg", ios::binary);
string myString;
ofstream fout("E:\\mnmn.txt");
while(getline(file,myString,'\n') )
{
for (size_t i = 0; i < myString.size(); ++i)
{
fout <<"."<< bitset<8>(myString.c_str()[i]);
}}
return 0;
}
Anyone can help ?
Change
while(getline(file,myString,'\n') )
{
for (size_t i = 0; i < myString.size(); ++i)
{
fout <<"."<< bitset<8>(myString.c_str()[i]);
}
}
to
int c;
while( (c = file.get()) != EOF )
{
fout <<"."<< bitset<8>(c);
}
Related
I try to capitalize every first letter of the words in a sentence.
Can someone explain me why I get some error after inputting word with space ex(asdasd asdasd).
#include <iostream>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char str[50];
char firstLetter;
int iASCII;
vector<string> vecString;
cin.getline(str, 100);
stringstream ss(str);
string sIndivWords;
char cSpace = ' ';
while (getline(ss, sIndivWords, cSpace))
{
vecString.push_back(sIndivWords);
}
iASCII = vecString[0].at(0);
if (iASCII >= 97 && iASCII <= 122)
{
for (int i = 0; i < vecString.size(); i++)
{
firstLetter = vecString[i].at(0);
putchar(toupper(firstLetter));
for (int j = 1; j < 20;j++)
{
cout << vecString[i].at(j);
}
}
}
}
As others have stated in their comments, your (non-)magic number of 20 is the problem, here:
for (int j = 1; j < 20;j++) {
cout << vecString[i].at(j);
}
This will crash when vecString[i] has less than 20 characters!
Try using something more appropriate, like:
for (int j = 1; j < vecString[i].length(); j++) {
cout << vecString[i].at(j);
}
As a side note, your attempt to use vecString.size() will fail, because that gives you the number of separate strings, not the length of any one string.
I need to create a function that counts chars in a string in C++, for example, when reading in from a text file that contains:
"aaabbbbbggssj"
the output should be:
"3a5b2g1j"
Im really not sure how to go about it and any help would be greatly appreciated! Thanks
My code is as follows:
#include <iostream>
using namespace std;
#include "Character.h"
#include <fstream>
#include <string>
using namespace std;
int main(){
int c;
void count(char [], int []);
//opening text file
string line = " ";
fstream my_stream;
my_stream.open("information.txt");
//loop to cout occurences of each character
while (getline (my_stream,line))
{
cout << line << '\n';
c += line.length();
cout << c;
int numOfChars = line.length();
for (unsigned int i = 0; i < line.length(); i++){
}
my_stream.close();
return 0;
}
Just count repeating symbols and produce result:
string compress(string const& str) {
string result = "";
int i = 0;
while (str[i]) {
int c = 1;
while (str[i] == str[i + c]) ++c;
result += std::to_string(c) + str[i];
i += c;
}
return result;
}
I need help debugging my code. I've tried many things, but I can't seem to be able to delete a character from a string.
I also don't understand completely how std::erase works, I'm not sure if you can erase characters with it.
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
char n;
cin >> s;
cin >> n;
for (int i = 0;i < s.length(); i++) {
s.erase (n[i]);
}
cout << s;
return 0;
}
EDIT: Sorry for being so vague. I recognized my issue of attempting to delete something from an array rather than the intended string. With the help of the answers posted; the updated code is attached below that works the way I want it to. Thank you for your responses!
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
char n;
cin >> s;
cin >> n;
for (int i = 0; i < s.length(); i++) {
while (s[i] == n) {
s.erase(i, i);
}
}
cout << s;
return 0;
}
Use the erase-remove idiom:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s = "Hello World!";
s.erase(std::remove(s.begin(), s.end(), 'l', s.end());
std::cout << s << std::endl;
return 0;
}
Broken down into two statements it would be:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s = "Hello World!";
auto it = std::remove(s.begin(), s.end(), 'l');
s.erase(it, s.end());
std::cout << s << std::endl;
return 0;
}
If you want just to remove one character from the string you can use its method find to find the character in the string. For example
auto pos = s.find( n );
if ( pos != std::string::npos ) s.erase( pos, 1 );
Or you can use a loop the following way
std::string::size_type pos = 0;
while ( pos < s.size() && s[pos] != n ) ++pos;
if ( pos != s.size() ) s.erase( pos, 1 );
If you want to erase all occurrences of the character in the string using a loop you can write
for ( std::string::size_type pos = 0; pos < s.size(); )
{
if ( s[pos] == n ) s.erase( pos, 1 );
else ++pos;
}
It will be helpful if you included a proper description of what you are trying to do. The question is a little vague but I presume you are trying to remove a given char from a string, if that's what you are trying to do, here is a working example that builds on what you already provided.
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
char n ;
cin >> s;
cin >> n;
for (int i = 0; i < s.length(); i++) {
if (s[i] == n) {
s.erase(i, 1);
}
}
cout << s;
return 0;
}
wanted to read only nums from file & store them in array.i don't know file handling. please help me out.
#include iostream conio.h
#stdio.h string fstream;
using namespace std;
int main()
{
ifstream myfile("ashishdata.txt");//file open
char c;
char arr[100];
int i=0;
// read data from file
while (myfile.get(c))
{
if(isdigit(c) || c=='.' )
{
arr[i] = c;
i++;
}
}
arr[i]='\0';//mark end of array
myfile.close();//file closed
int k;
k=strlen(arr);
//parsing string
int newarr[100];
int m=0;
string(s);
for(i=0;i<=k;i++)
{
s.erase();
while(arr[i] != '.')
{
s+=arr[i];
i++;
}
newarr[m]=atoi(s.c_str());
m++;
}
newarr[m]='\0';
//printing newarray
i=0;
while(newarr[i]!='\0')
{
cout<<newarr[i]<<'\t';
i++;
}
cout<<endl;
getch();
return(0);
}
Try this:
filename.txt:
4545.454
sds
65.65.6.65656
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char c;
char arr[100];
size_t i = 0;
// read data from file
ifstream myfile;
myfile.open("filename.txt", ios::in);
while (!myfile.eof()) {
myfile >> c;
if (isdigit(c) || c == '.') {
arr[i] = c;
i++;
}
}
arr[i] = '\0'; // mark end of array
myfile.close(); // file closed
size_t k = strlen(arr);
// parsing string
int newarr[100];
int m = 0;
string s;
for (i = 0; i <= k; i++) {
s.erase();
while (arr[i] != '.') {
s += arr[i++];
}
newarr[m++] = atoi(s.c_str());
}
newarr[m] = '\0';
// printing newarray
i = 0;
while (newarr[i] != '\0') {
cout << newarr[i++] << '\t';
}
return 0;
}
I have no idea why ios::right works once. totally nothing
Same problem with ios::hex, ios::decimal and a few others unless I do some insane codes and get them magically work again
#include <iostream>
#include <iomanip>
using std::cout;
using std::ios;
int main() {
int len;
std::cin >> len;
// w = len + 1;
cout.width(len);
cout.setf(ios::right);
for (int s = 0; s < len; s++) {
for (int c = 0; c <= s; c++) {
cout << '#';
}
cout << '\n';
}
std::cin.get();
std::cin.get();
}
Expected Output:
#
##
###
####
#####
######
What i get:
#
##
###
####
#####
######
Tried This:
cout << ios::right << '#';
Didn't work.
You need to write cout.width(len-s);cout.setf(ios::right); inside the first loop because ios::right works only for one time. So it should be
#include <iostream>
#include <iomanip>
using std::cout;
using std::ios;
int main()
{
int len;
cin >> len;
for (int s = 0; s < len; s++)
{
cout.width(len);
cout.setf(ios::right);
for (int c = 0; c <= s; c++)
{
cout<<"#";
}
cout<<"\n";
}
std::cin.get();
std::cin.get();
}
But your code is not correct as per you required output , correct code is :
#include <iostream>
#include <iomanip>
using std::cout;
using std::ios;
int main()
{
int len;
cin >> len;
for (int s = 0; s < len; s++)
{
cout.width(len-s); // to align properly
cout.setf(ios::right);
for (int c = 0; c <= s; c++)
{
cout<<"#";
}
cout<<"\n";
}
std::cin.get();
std::cin.get();
}
The problem here is not that right is temporary, but that the width is temporary, so the next character being output does not use the width given - in fact that's a good thing, or your second line would be # #, so you probably don't want that!
The solution is to move the width call into the outer of your two loops (and recalculated the width accordingly as you get wider and wider output).
I'm intentionally not writing how you should do this, as you need to practice thinking about how thing work, not practicing CTRL-C/CTRL-V.
A shorter and simpler way of doing what you want
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = n; j > 0; --j)
cout << (i >= j ? "#" : " ");
cout << endl;
}
return 0;
}
Input
6
Output
#
##
###
####
#####
######
See http://ideone.com/fbrfpe demo.