簡體   English   中英

有關 function "strcpy'' 的規格和可能的解決方案的詳細信息

[英]Details about the specification of the function "strcpy'' and possible solution

我想將字符串從復制到strs

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

#define N 100
void readAndWrite();

int main() {
    readAndWrite();
    return 0;
}

void readAndWrite() {
    fstream in;
    char* strs[100];
    for (int i = 0; i < N; i++) {
        strs[i] = (char*)calloc(128,sizeof(char));
    }

    in.open("A.in");
    if (!in) {
        cout << "fail" << endl;
        exit(-1);
    }

    char line[128];
    in.getline(line, 128);
    **strcpy(strs[0], line);**
    cout << "line:" << strs[0] << endl;

    int count = 1;
    while (!in.eof()) {
        in.getline(line, 128);
        **strcpy(strs[count++], line);**
        cout << "line:" << strs[count - 1] << endl;
    }
}

我收到一份報告說 strcpy 不安全,這表明

“strs [0]”可能是“0”:這不符合function“strcpy”的規范。

我怎樣才能使我的代碼工作

您忘記檢查calloc的返回值,這就是編譯器告訴您它可能為零(null)的原因,以防沒有足夠的 memory 可供分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM