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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// 16a3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "16a3.h"
using namespace std;
const int FILE_NAME_SIZE = 100;
const int MAX_ARRAY_SIZE = 10;
void getFileName(char fileName[FILE_NAME_SIZE]);
int getLines(string lines[], FILE* file);
void output(string lines[], int count);
int main()
{
FILE* file;
string lines[MAX_ARRAY_SIZE] = {};
char fileName[FILE_NAME_SIZE];
int count;
getFileName(fileName);
fopen_s(&file, fileName, "r");
count = getLines(lines, file);
fclose(file);
output(lines, count);
}
void getFileName(char fileName[FILE_NAME_SIZE])
{
cout << "Input input the file name to read from: ";
cin.getline(fileName, FILE_NAME_SIZE, '\n');
while (!strcmp(fileName, ""))
{
cout << "Unknown file.";
cout << "Input input the file name to read from: ";
cin.getline(fileName, FILE_NAME_SIZE, '\n');
}
}
int getLines(string lines[], FILE* file)
{
int count = 0;
char temp[100];
while (feof(file) == 0) {
fscanf_s(file, "%[^\n]\n", temp, sizeof(temp));
strcat_s(temp, "\0");
string str(temp);
lines[count] = str;
count++;
}
/*
while (!feof(file))
{
int out = fscanf_s(file, "%[^\n]\n", &lines[count]);
count++;
}*/
return count;
}
void output(string lines[], int count)
{
cout << "\n\n";
for (int i = 0; i < count; i++)
{
cout << i << " " << lines[i] << " " << lines[i].length() << endl;
}
}
|