我在一个定义为的类中有一个私有属性vector<pair<char *, int> > data;.我用这个向量添加数据data.push_back(make_pair(p, r));.后来当我从矢量中获取数据时,我得到了p值的坏数据.返回的数据就像??U3.我认为这是因为存储了一个指向char数组的指针.我将如何在矢量中存储实际副本.如果它有助于char数组永远不会超过255个字符+ 1为null终止.
如何使用WriteFile Win32 API函数将CString实例的内容写入CreateFile打开的文件?
请注意不使用MFC,包含"atlstr.h"使用CString
编辑:我可以
WriteFile(handle, cstr, cstr.GetLength(), &dwWritten, NULL);
Run Code Online (Sandbox Code Playgroud)
要么
WriteFile(handle, cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL);
Run Code Online (Sandbox Code Playgroud)
?
我用C++编程.
这个问题基本上是我无法在任何地方找到答案.所以这是问题所在:
我想创建一个C风格的字符串,但我想将一个整数变量i放入字符串中.很自然地,我使用了一个流:
stringstream foo;
foo
<< "blah blah blah blah... i = "
<< i
<< " blah blah... ";
Run Code Online (Sandbox Code Playgroud)
但是,我需要以某种方式获得一个C风格的字符串传递给一个函数(原来foo.str()返回一个std :: string).所以这在技术上是一个三部分问题 -
1)如何将std :: string转换为C风格的字符串?
2)有没有办法从字符串流中获取C风格的字符串?
3)有没有办法直接使用C风格的字符串(不使用字符串流)来构造一个带有整数变量的字符串?
我正在尝试读取名为的文件argv[1],但我不知道我是如何做到的.感觉它很简单,编译时我得到的错误信息是,
main.cpp: In function ‘void* ReadFile(char**, int&)’:
main.cpp:43:22: error: request for member ‘c_str’ in ‘*(argv + 8u)’, which is of non-class type ‘char*’
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include "movies.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void *ReadFile(char*[], int&);
int size;
int main(int argc, char *argv[])
{
char * title[100];
cout << argv[1] << endl;
//strcpy(argv[1],title[100]);
//cout << title << endl;
ReadFile(argv , size);
return 0;
}
void *ReadFile(char * argv[] , int& size) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将32字节字符串的前16个字节复制到dest。
unsigned char src[32] = "HELLO-HELLO-HELLO-HELLO-HELLO-12";
unsigned char dest[16];
memcpy(dest, src, 16); // COPY
printf("%s\n", src);
printf("%lu\n", strlen(src));
printf("%s\n", dest);
printf("%lu\n", strlen(dest));
Run Code Online (Sandbox Code Playgroud)
输出如下
HELLO-HELLO-HELLO-HELLO-HELLO-12
32
HELLO-HELLO-HELLHELLO-HELLO-HELLO-HELLO-HELLO-12
48
Run Code Online (Sandbox Code Playgroud)
我期待收到HELLO-HELLO-HELL的dest唯一。的前16个字节dest实际上包含预期的结果。
为什么dest超出其实际承受能力?为什么长度为16+32=48?有没有办法只复制前16个字节的src到dest?
我可以将NSMutableString转换为char*吗?
例如
NSString* someval = @"This is a test.";
NSMutableString *temp = [NSMutableSTring stringWithString:someval];
char * temp2 = ????Not sure what to do here???;
Run Code Online (Sandbox Code Playgroud) 当printf("%s/%s\n", str1, str2);打印出字符串而sprintf(str3, "%s/%s", str1, str2);导致程序崩溃时,我可能忽略了一些简单的事情吗?
这只发生在我在VirtualBox上运行的Ubuntu(最新版本)上.在Windows(主机)上,两条线路都没有任何问题.
如果需要更多背景信息,请告诉我.我希望我错过了一些愚蠢的事情,那两条线路绰绰有余.
我的功能有这个原型:
char[][100] toArray(char document[]);
Run Code Online (Sandbox Code Playgroud)
g ++ on cygwin返回此错误:
Unable to resolve identifier toArray
Run Code Online (Sandbox Code Playgroud)
如何返回C-Strings数组?
我正在理解strcmp函数的奇怪行为,将通过以下代码进行说明:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *p = "no";
cout << p << endl; //Output: no
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0
cin >> p; //Input: bo
cout << p << endl; //Output: bo
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,它接收一个字符串作为参数,然后完全修改它:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void updatePEP(char **pep_, int v){
if((v == 0)){
//pep_[0] = '1';
//strncpy(pep_[0],"1", 2);
strncpy(*pep_,"10", 2);
//*pep_ = "10";
}
else if((v < 0)){
strncpy(*pep_,"01", 2);
}
}
int main(){
char *PEP = "00";
int i = 0, j = -1;
printf("%s\n",PEP);
updatePEP(&PEP,i);
printf("%s\n",PEP);
updatePEP(&PEP,j);
printf("%s\n",PEP);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经在互联网上搜索了,我相信我正确地将字符串作为参考,所以我怀疑是否使用:
pep_[0] = '1';
strncpy(pep_[0],"1", 2);
strncpy(*pep_,"10", 2);
*pep_ = "10";
Run Code Online (Sandbox Code Playgroud)
为什么?(但它们都不起作用,所以它们也可能是错的...)