The command to compile in Ubuntu Terminal is g++ scrap.c -std=c++11
I looked at several other questions with the same error but none use strcat which is where the exception occurs for me.
I am trying to copy all chars from test1 into temp[] in chunks of 512 chars at a time, but I am getting an exception Access violation reading location 0x....
Another thing I'm trying to do is load temp into an item of fileArray[] but the strcpy at the bottom of my code is giving me no suitable conversion from str to "char *" exists.
I tried hard to give a "complete, minimal, verifiable example" since admins seem to be pushing that really hard right now; please let me know if I did it right here. You should just be able to copy my code and compile it with g++ to check it.
scrap.c (Updated):
//#include <stdafx.h>
//#include "stdafx.h"
#include <stdio.h> /* defines FILENAME_MAX */
#include <assert.h>
#include <iostream>
#include <fstream>
#include <cstring>
//#include <filesystem>
#include <experimental/filesystem>
#include "dirent.h"
//#include <direct.h>
using namespace std;
int main() {
string test1 = "This is test1.txt...This is test1.txt...";
std::vector<std::string> fileArray[10];
string temp = "";
for (int a = 0; a < 10; a++) {
int block = 512 * a;
int currentBlockPosition = 0;
while (currentBlockPosition < 512 && (currentBlockPosition + block) < test1.size()) {
temp += test1.at(block + currentBlockPosition);
cout << "block + currentBlockPos: " << block + currentBlockPosition << endl;
cout << "current char: " << test1.at(block + currentBlockPosition) << endl;
currentBlockPosition++;
}
temp = "";
cout << "temp: " << temp << endl;
cout << "a: " << a << endl;
//fileArray[a] = temp; // no operator "=" matches these operands ... operand types are: std::vector<std::string, std::allocator<std::string>> = std::string
}
}
Found the solution.
The access violation error had to do with me using a char [] instead of a string.
Using fileArray->push_back(temp); turned out to be the proper syntax instead of fileArray[a] = temp.
Also, I had to add in the second condition in the while() to avoid the out of bounds error.
Related
I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"D:\\testlas\\BigOne.pcd",(char*)"D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?D:\\testlas\\BigOne.pcd
m.name: ?D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.
i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}
The maxPointers value may need to be different for your system, but allocating many unique_ptrs causes this application to crash and burn. Removing the definition of s and the cin operation gives some more room for pointer allocation.
Using MSVC 2015.
So, why does it crash and how to avoid it?
Thanks.
#include <iostream>
#include <vector>
#include <string>
#include <memory>
using namespace std;
int main(int argn, const char*argv[])
{
int maxPointers = 37900;
vector<unique_ptr<string>> pointerHolder;
for (int i = 0; i < maxPointers; i++)
{
pointerHolder.push_back(make_unique<string>("pointer " + i));
}
cout << "done creating "<< maxPointers << " pointers" << endl;
string s;
cin >> s;
for (int i = 0; i < maxPointers; i++)
{
pointerHolder.at(i).release();
}
pointerHolder.clear();
cout << "done releasing " << maxPointers << " pointers" << endl;
return EXIT_SUCCESS;
}
The crash you encounter is because you build strings from garbage that results from call "pointer " + i. If you intend to concatenate literal "pointer" with an integer, then you'd need to convert that integer to std::string with std::to_string first:
make_unique<string>("pointer " + to_string(i));
// ~~~~~~~~~~~^
i'm having problem with this code :
#include <iostream>
#include <math.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
using std::cout; using std::cerr;
using std::cin; using std::string;
using std::endl;
int main(int argc,char* argv[])
{
for(int x = 0; x <= 2013; x++)
{
cout << "Iteration: "<< x << "\r";
cout << "";
}
return 0;
}
i need my code to print "Iteration: 0" and than just refresh that 0 to 1,2,3,4.... everything on one console line. I used the carriage return but it doesn't work,the line are printed one after another as when i use "\n". The enviroment is linux 64 bit. The IDE is eclipse 8.01.
You have to put it at the beginning of the line:
cout << "\rIteration: "<< x;
EDIT: I have tested this modification of the original OP's code and it prints what he wants. Also, Oh dear look what I've found.
Also, as suggested by #Wintermute, you can do the following inside the for loop, for better visualization:
cout << "\rIteration: "<< x << std::flush;
sleep(1);
On the following program, I'm getting this when I attempt to use cout to output a C++ string to stdout - the other instructions produce the expected output. I'm using MS Visual Studio 2010 on a Windows 7 system.
First-chance exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD: Stack
overflow. Unhandled exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD:
Stack overflow. The program '[3740] Lab1.exe: Native' has exited with
code -1073741571 (0xc00000fd).
#include "StdAfx.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <string>
#include <ctime>
//more code here
int main() {
int number = 1;
string myStr = "Hello, string!";
cout << "number: " << number << endl;
cout << "Hello, World!" << endl;
cout << myStr << endl; //failing instruction
cout << "\nHit any key to continue...." << endl;
cin.get();
return 0;
}
My instructor suggested changing the failing instruction to use data() or c_str() like so:
cout << myStr.data() << endl;
I did this, and this resolved the problem. He didn't know why, just said it worked so not to worry about it.
It seems to me that a C++ ostream object like cout should be able to handle a C++ string. Am I missing something, or do I really need to use data() or c_str() with cout?
I also tried using std::cout, std::string, and std::endl - it didn't help.
Thanks in advance for your advice; I'm really wanting to understand what's going on here.
Helen
You should include string instead of string.h:
#include <string>
I doubt that cout << myStr << endl; was the troublesome line.
This code works fine:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s("Hello World!");
cout << s << endl;
return 0;
}
The error message indicates that you have a stack overflow: it seems some function is being called recursively. You didn't define your own output function for string by any chance? What is in "more code here" which may be related to output operators?