字符串输入未在屏幕上正确打印
string input not printed correctly on the screen
我正在尝试从用户输入中读取一个字符串,然后将其打印在屏幕上。但是,当字符串打印在控制台上时,它有点乱码。有趣的是,它在 Visual Studio 而不是 CodeBlocks 中有效。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int i, num_bytes;
char sentence[] = "";
std::cout << "Please enter your name: ";
//fgets(sentence, 100, stdin);
//scanf("%[^\n]%*c", sentence);
//scanf("%[^\n]", sentence);
std::cin >> sentence;
num_bytes = strlen(sentence);
LPVOID ptr = VirtualAlloc(NULL, num_bytes, MEM_RESERVE, PAGE_READWRITE);
ptr = VirtualAlloc(ptr, num_bytes, MEM_COMMIT, PAGE_READWRITE);
if (ptr) {
char* char_ptr = static_cast<char*>(ptr);
for (i = 0; i < num_bytes; i++) {
char_ptr[i] = sentence[i];
}
std::cout << "Allocated Memory Address: " << (void *)ptr << std::endl;
std::cout << "Press Enter to print out the characters.\n";
getchar();
for (i = 0; i < num_bytes; i++) {
std::cout << char_ptr[i];
}
std::cout << "\nPress Enter to clear memory." << std::endl;
getchar();
VirtualFree(ptr, 0, MEM_RELEASE);
} else {
std::cout << "Could not allocate " << num_bytes << " of memory." << std::endl;
}
std::cout << "\nPress Enter to continue." << std::endl;
getchar();
}
您应该明确指定字符串的内存大小。
该代码:
char sentence[] = "";
声明句子的最大长度为 0(+1 个零符号)。当然,您将更多数据写入非您的内存中。
试试这个:
char sentence[200] = "";
而不是这个:
char sentence[] = "";
std::cout << "Please enter your name: ";
//fgets(sentence, 100, stdin);
//scanf("%[^\n]%*c", sentence);
//scanf("%[^\n]", sentence);
std::cin >> sentence;
num_bytes = strlen(sentence);
你最好这样写:
constexpr std::streamsize BUFFER_SIZE { 100 };
std::array<char, BUFFER_SIZE> sentence { }; // use the more modern std::array
std::cout << "Please enter your name: ";
std::cin.getline( sentence.data( ), BUFFER_SIZE ); // will only read 100 chars
// including spaces and tabs
num_bytes = strlen(sentence.data( ));
其余与您的代码相同。但是现在用户也可以输入 space 个字符,并且它们不会导致缓冲区溢出,因为 std::cin.getline
只会从标准输入流中读取有限数量的字符。
我正在尝试从用户输入中读取一个字符串,然后将其打印在屏幕上。但是,当字符串打印在控制台上时,它有点乱码。有趣的是,它在 Visual Studio 而不是 CodeBlocks 中有效。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int i, num_bytes;
char sentence[] = "";
std::cout << "Please enter your name: ";
//fgets(sentence, 100, stdin);
//scanf("%[^\n]%*c", sentence);
//scanf("%[^\n]", sentence);
std::cin >> sentence;
num_bytes = strlen(sentence);
LPVOID ptr = VirtualAlloc(NULL, num_bytes, MEM_RESERVE, PAGE_READWRITE);
ptr = VirtualAlloc(ptr, num_bytes, MEM_COMMIT, PAGE_READWRITE);
if (ptr) {
char* char_ptr = static_cast<char*>(ptr);
for (i = 0; i < num_bytes; i++) {
char_ptr[i] = sentence[i];
}
std::cout << "Allocated Memory Address: " << (void *)ptr << std::endl;
std::cout << "Press Enter to print out the characters.\n";
getchar();
for (i = 0; i < num_bytes; i++) {
std::cout << char_ptr[i];
}
std::cout << "\nPress Enter to clear memory." << std::endl;
getchar();
VirtualFree(ptr, 0, MEM_RELEASE);
} else {
std::cout << "Could not allocate " << num_bytes << " of memory." << std::endl;
}
std::cout << "\nPress Enter to continue." << std::endl;
getchar();
}
您应该明确指定字符串的内存大小。 该代码:
char sentence[] = "";
声明句子的最大长度为 0(+1 个零符号)。当然,您将更多数据写入非您的内存中。 试试这个:
char sentence[200] = "";
而不是这个:
char sentence[] = "";
std::cout << "Please enter your name: ";
//fgets(sentence, 100, stdin);
//scanf("%[^\n]%*c", sentence);
//scanf("%[^\n]", sentence);
std::cin >> sentence;
num_bytes = strlen(sentence);
你最好这样写:
constexpr std::streamsize BUFFER_SIZE { 100 };
std::array<char, BUFFER_SIZE> sentence { }; // use the more modern std::array
std::cout << "Please enter your name: ";
std::cin.getline( sentence.data( ), BUFFER_SIZE ); // will only read 100 chars
// including spaces and tabs
num_bytes = strlen(sentence.data( ));
其余与您的代码相同。但是现在用户也可以输入 space 个字符,并且它们不会导致缓冲区溢出,因为 std::cin.getline
只会从标准输入流中读取有限数量的字符。