Orangele's Blog.

Practical IO for C++

Word count: 599Reading time: 3 min
2019/07/09 Share

Here I’m going to summarize some common techniques for IO, especially for type conversion and spliting the string, which are usually the most annoying parts in IO.

Read a file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(){
ifstream fin("data.txt");

//read by lines
string str;
if(fin){
while(getline(fin,str)){ //this "getline" is in <string>
cout<<str<<endl;
}
}

//set the read pointer to the beginning of the f
fin.clear();
fin.seekg(0, ios::beg)

//read by words(str)
if(fin){
while(fin>>str){
cout<<str<<endl;
}
}

return 0;
}

Note: getline sets the ios::fail when count-1 characters have been extracted. So, before reseting the read pointer fin.clear() must be called.

Converting the type

String to int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include<sstream>

using namespace std;

int main(){
string str = "123456";

stringstream ss(str);
int x = 0;
ss >> x;

cout<<"The number is: " << x << endl;

return 0;
}

String to char array

  • Approach 1:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include <cstring>
    #include <iostream>

    using namespace std;

    int main(){
    string str = "fuck";

    int n = str.length();
    char char_array[n+1];

    strcpy(char_array, str.c_str());

    for(int i = 0; i < 0; i++)
    cout << char_array[i] << endl;

    return 0;
    }

    Note: The c_str() function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value of the string. So the length of str.c_str() is str.length()+1.

  • Approach2:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <iostream>

    using namespace std;

    int main(){
    string str = "fuck";

    char char_array[str.length()];

    for(int i = 0; i < 0; i++){
    char_array[i] = str[i];
    cout << char_array[i] << endl;
    }

    return 0;
    }

char array to int array(by letter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main(){
char* chr = ['1','2','3'];

int int_arr[strlen(chr)];

for (int i=0; i<strlen(chr); i++){
int_arr[i] = chr[i] - '0';
cout << int_arr[i] << endl;
}

return 0;
}

Note: If we want the length of an int array, we need use sizeof(int_arr)/sizeof(int).

char array to int array(by word)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){
char* chr = ['1','2','3'];

int num = atoi(chr);

cout << num << endl;

return 0;
}

Split the string

Parse a comma-delimited string into int array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
string str = "1,2,3,4,5,6";
vector<int> int_vec;
stringstream ss(str);

int i = 0;

while (ss >> i){
int_vec.push_back(i);

if (ss.peek() == ',')
ss.ignore();
}

for (i=0; i< int_vec.size(); i++)
cout << int_vec.at(i)<<endl;

return 0;
}

Parse a comma-delimited string into string array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main(){
string str = "1,1,1,1, or something else ,1,1,1,0"
stringstream ss(str);
vector<string> result;

while(ss.good()){
string substr;
getline(ss, substr, ',');
result.push_back(substr);
}

return 0;
}
CATALOG
  1. 1. Read a file
  2. 2. Converting the type
    1. 2.1. String to int
    2. 2.2. String to char array
    3. 2.3. char array to int array(by letter)
    4. 2.4. char array to int array(by word)
  3. 3. Split the string
    1. 3.1. Parse a comma-delimited string into int array.
    2. 3.2. Parse a comma-delimited string into string array.