Filling a cstring array with text from a text file

Hello all, I wanted to try out the cstrings and decided to use them with File I/O in c++. I want to fill a c-string array with text and then print out how many spaces, vowels, uppercase letters, lowercase letters. But before I can do any of that my file only fills the array with the last word from the statement.
the statment is

"Hello World from me
I like to program"

it only prints program in a for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

int main() {

    ifstream myfile;
    myfile.open("output.txt");

    //check for error
    if (myfile.fail()) {
        cout << "Error opening file: ";
        exit(1);
    }
    char statement[100];
    while (!myfile.eof()) {
        myfile >> statement;
    }
    for (int i = 0; i < 20; ++i) {
        cout << statement << " ";
    }
}
You are continually replacing the input each time with the new input.
Hello Sir, I don't understand your answer.
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 <fstream>
#include <iostream>
#include <cstring>

using namespace std;

int main() {

    ifstream myfile;
    myfile.open("output.txt");

    //check for error
    if (myfile.fail()) {
        cout << "Error opening file: ";
        exit(1);
    }
    char statement[100];
    int i = 0;
    while (myfile >> std::noskipws >> statement[i] and i < 100) // <---
    {
        cout << statement[i] << '\n';
        
        i++;
    }
    
    cout << statement << " ";
}


Check this out as a way of doing what you want. It reads the file character by character and keeps reading until there are no more characters left instead of looking for the end of the file. Also it reads spaces and adds them to the string instead of ignoring them.
Sir, I am afraid I don't understand what std::noskipws is, I am fairly new to Programming with no background experience at all. Hope you can keep it beginner friendly
It should be fairly obvious from what I wrote but perhaps google noskipws?

(The prefix "std::" can be ignored because you have "using namespace std;")
I did not learn it yet so thats why I am asking why doesn't while(!myfile.eof(()) work? it works usually.
Sorry for jumping off, what I meant was that every time you input a value, it takes that value and swaps what is in the cstring with it. This coupled with the fact that you are reading in the file a word at a time causes it to just drop most of the file until the last word.


With your input file, this is what it would look like:

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
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

int main() {

    ifstream myfile;
    myfile.open("output.txt");

    //check for error
    if (myfile.fail()) {
        cout << "Error opening file: ";
        exit(1);
    }
    char statement[100];
    while (!myfile.eof()) {
        myfile >> statement; // replaces data in cstring with new data. Doesn’t append.
        std::cout << statement << " , "; 
    }
/*
    for (int i = 0; i < 20; ++i) {
        cout << statement;
    }
*/
}
Hello , World , from , me , I , like , to , program , 
Last edited on
Well the problem with std::skiparys is that it only reads the first letter of my statements, ends the line starts the new line, prints first letter only and so on... I want to print it the way it is 2 lines and same number of spaces
So do it this way, eof() reservations notwithstanding:

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
#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;

int main() {

    ifstream myfile;
    myfile.open("output.txt");

    //check for error
    if (myfile.fail()) {
        cout << "Error opening file: ";
        exit(1);
    }
    char statement[100];
    int i = 0;
    while (!myfile.eof() and i < 100)
    {
        myfile >> std::noskipws >> statement[i];
        cout << i << ' ' << statement[i] << '\n'; // <-- line is just for fun
        
        i++;
    }
    
    cout << statement << " ";
}


0 H
1 e
2 l
3 l
4 o
5  
6 W
7 o
8 r
9 l
10 d
11  
12 f
13 r
14 o
15 m
16  
17 m
18 e
19 

20 I
21  
22 l
23 i
24 k
25 e
26  
27 t
28 o
29  
30 p
31 r
32 o
33 g
34 r
35 a
36 m
37 

38 
Hello World from me
I like to program
 Program ended with exit code: 0



output.txt is 2 lines:
Hello World from me
I like to program


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
#include <fstream>
#include <iostream>
#include <cstring>

int main()
{

   std::ifstream myfile("output.txt");

   //check for error
   if (myfile.fail())
   {
      std::cout << "Error opening file....\n";
      exit(1);
   }

   char statement[100] { };
   char line[100]      { };

   while (myfile.getline(line, 100))
   {
      strcat_s(statement, 100, line);
   }

   std::cout << statement << '\n' << strlen(statement) << '\n';
}

"Hello World from me I like to program"
39
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 <fstream>
#include <iostream>

using namespace std;

int main()
{
    
    ifstream myfile;
    myfile.open("output.txt");
    
    //check for error
    if (myfile.fail())
    {
        cout << "Error opening file: ";
        exit(1);
    }
    char statement[100]{};
    int i = 0;
    
    while (myfile.get(statement[i]) and i < 100)
    {
        i++;
    }
    
    cout << statement << '\n';
}
Last edited on
Topic archived. No new replies allowed.