使用strcpy时C 访问违规读取地点
C++ Access violation reading location when using strcpy
在Ubuntu终端中编译的命令是g++ scrap.c -std=c++11
我查看了其他错误的问题,但没有一个使用strcat
,这是我发生异常的地方。
我试图将所有字符从test1
复制到一次512个字符中的temp[]
中,但是我得到了一个例外Access violation reading location 0x...
。
我要做的另一件事是将温度加载到fileArray[]
的项目中,但是代码底部的strcpy
是给我no suitable conversion from str to "char *" exists
。
我努力给出一个"完整,最小,可验证的例子",因为管理员似乎现在真的很难推动这一点。请让我知道我是否在这里做。您应该能够复制我的代码并使用G 进行编译以检查它。
scrap.c(更新(:
//#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
}
}
找到了解决方案。
使用char []
而不是string
,访问违规错误与我有关。
使用fileArray->push_back(temp);
证明是正确的语法而不是fileArray[a] = temp
。
另外,我必须在while()
中添加第二个条件,以避免界限错误。
相关文章:
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 读取访问冲突.这0xCDCDCDCD
- 尝试通过共享指针使用变量时读取访问冲突
- C++ 读取访问冲突,0xCDCDCDCD
- 从二进制文件中读取元素时引发异常(引发异常:读取访问冲突. _Pnext 0xB414D4)
- 使用智能指针读取访问冲突
- 引发异常:读取访问冲突.**此**0x40错误
- 函数 AfxEndThread 中的读取访问冲突"pThread-> was nullptr"
- Visual Studio图形调试器引发读取访问冲突异常
- C++中的内存分配(引发异常:读取访问冲突)
- 引发异常:读取访问冲突 [C++]
- 尝试从单向链表C++中删除单个节点时出现读取访问冲突
- 引发未经处理的异常:简单 C++ 程序中的读取访问冲突,动态增加数组长度以存储数字
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- 读取访问冲突. _Mycont 在 C++ 中将 udp 数据放入向量时出现 nullptr 错误
- 读取访问冲突0xCDCDCDCD
- Vector::p ush_back() 给出读取访问冲突
- 访问共享内存时出现读取访问冲突
- 挂钩创建文件抛出异常:读取访问冲突
- 在字符串驱动器中读取访问违规