2 분 소요

자주 사용하는데 완벽하게 사용하지 못한 기술들

  1. malloc
  2. 포인터
  3. [문자열 배열](#3문자열-배열
  4. 파일 읽고 쓰기

malloc

  1. 기본 적인 사용법
    int arr[4] = { 4,3,2,1 };        //크기가 4인 배열 선언
    int* pArr;                        //int 타입을 가리키는 포인터 pArr 선언

    //int 타입사이즈 * 4 만큼의 메모리를 할당해라!
    pArr = (int*)malloc(sizeof(int) * 4);

    if (pArr == NULL)
    {
        //메모리 할당에 실패하면 이쪽으로 들어옵니다.
        printf("malloc error");
    }

    for (int i = 0; i < 4; ++i)
    {
        pArr[i] = arr[i];
    }

    for (int i = 0; i < 4; ++i)
    {
        printf("%d \n", pArr[i]);
    }

    //할당한 메모리를 해제해줍니다.
    free(pArr);

    system("pause");

2.포인터

  1. 포인터 ```C++ #include #include

using namespace std;

void add(int a, int b, int* sum) { *sum = a + b; cout «“함수내 공간” « sum « endl; }

int main(int argc, char* argv[], char* envp[]) { bool numberLines = false; // Default is no line numbers.

// If /n is passed to the .exe, display numbered listing
// of environment variables.
if ((argc == 2) && _stricmp(argv[1], "/n") == 0)
    numberLines = true;
   
int a = 3;
int b = 3;

int sum = 0;

add(a, b, &sum);

cout << sum << "test" << &sum << endl;
system("pause");
return 0; }       ```

3.문자열 배열

배열은 첫번쨰 안에 넣어서 사용한다.

char *str = "Hello";


4. 파일 읽고 쓰기

#include 헤더 이용하여 습니다

파일 읽기 (특정 앞에 값이 같은 내용 읽기 예시 )


vector<string> split_custom(string str, char delimiter) {
    vector<string> internal;
    stringstream ss(str);
    string temp;

    while (getline(ss, temp, delimiter)) {
        internal.push_back(temp);
    }

    return internal;
}

vector<string> ReadData(int i_linenumber)
{
    fstream read_file("fileIn.txt"); //File to read from
    vector<string> lines;
    vector<string> v_str_Result;
    string line;
    if (read_file.fail())
    {
        cout << "Error opening file." << endl;
        return v_str_Result;
    }


    while (getline(read_file, line))
    {
        lines =split_custom(line, ' ');
        if (stoi(lines[0]) == i_linenumber)
        {
            v_str_Result.push_back(line);
        }
    }

    read_file.close();

    return v_str_Result;

}


파일 쓰기

void WriteData(string str_data)
{
    // 파일 읽기 준비
    /* 중복 데이터 수집 관련 수정 필요 */ 
    fstream dataFile;
    string buffer;

    dataFile.open("fileIn.txt", ios::app);
    dataFile << str_data <<endl;
    dataFile.close();    
 }

파일 수정하기

void ModifyData(int line_number, string data_2)
{
    fstream read_file("fileIn.txt"); //File to read from
    if (read_file.fail())
    {
        cout << "Error opening file." << endl;
        return;
    }
    vector<string> lines;
    string line;
    while (getline(read_file, line))
        lines.push_back(line);

    read_file.close();
    if (line_number > lines.size())
    {
        return;
    }

    // Create ofstream object for writing to the file
    ofstream write_file;

    // Open the file with the provided filename
    write_file.open("fileIn.txt");

    // If the file failed to open, exit with an error message and exit status
    if (write_file.fail())
    {
        cout << "Error opening file." << endl;
        return;
    }

    line_number--;

    for (int i = 0; i < lines.size(); i++)
        if (i != line_number)
            write_file << lines[i] << endl;
        else
            write_file << data_2 << endl;

    // Close our access to the file since we are done working with it
    write_file.close();
}


파일 삭제하기

void DeleteLineFile(int line_number)
{
    fstream read_file("fileIn.txt"); //File to read from
    if (read_file.fail())
    {
        cout << "Error opening file." << endl;
        return ;
    }
    vector<string> lines;
    string line;
    while (getline(read_file, line))
        lines.push_back(line);

    read_file.close();
    if (line_number > lines.size())
    {
        return;
    }

    // Create ofstream object for writing to the file
    ofstream write_file;

    // Open the file with the provided filename
    write_file.open("fileIn.txt");

    // If the file failed to open, exit with an error message and exit status
    if (write_file.fail())
    {
        cout << "Error opening file." << endl;
        return ;
    }

    line_number--;

    for (int i = 0; i < lines.size(); i++)
        if (i != line_number)
            write_file << lines[i] << endl;

    // Close our access to the file since we are done working with it
    write_file.close();
}


관련 링크

  1. malloc https://blockdmask.tistory.com/290

  2. C언어 공부용 http://www.tcpschool.com/c/c_pointer_intro

https://jhnyang.tistory.com/100

태그:

카테고리:

업데이트:

댓글남기기