in Education by
I am simply trying to read each character from a file and print them in the screen. For testing, I tried to print ascii value in a console screen first before printing characters. the content of the file I am trying to read is below: assign1_2.cpp:33:20: error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)' I used the code below #include #include #include #include #include using namespace std; void CountLetters(string filename); int main() { CountLetters("count.txt"); } void CountLetters(string filename) { cout << filename << endl; ifstream in; in.open(filename.c_str(), ios::in); vector<char> letter; char temp; while (!in.eof()) { cout << in.get() << endl; } in.close(); } After running these code and I see "-1" at the end in the console screen. Anyone please explain? thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Do not read while not eof()1. That's not a proper reading loop. Read while reading succeeds. int x; while ((x = in.get()) != EOF) { cout << x << endl; } Testing for in.eof() will not guarantee reading will succeed. When you test for in.eof() you're actually testing if the previous read operation tried to read past the end of the file. This is bad, because it means the previous read operation failed. It failed and you didn't care, and just pressed on to use the value it returned even though it failed. When in.get() fails, it returns the constant EOF. That's what you should be checking. If in.get() fails, you don't want to continue with your loop as if it succeeded. 1 Same goes for while good() or while not bad().

Related questions

0 votes
    I am simply trying to read each character from a file and print them in the screen. For testing, I tried to print ... ("count.txt"); } void CountLetters(string filename) { cout...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    So I'm basically finished with a program, and at the end of it I'm printing Strings from an array to a file. The array may ... the code I'm using to check for null for(int i=0;i...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a class which has 2 sets of getter & setters. 1 set is the traditional type. These work ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I have a Docker container with MariaDB installed. I am not using any volumes. [vagrant@devops ~]$ sudo ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    computer is an electronic device and can only understand the on and off state of a switch, this two state are ... very very very urgent Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Calculate the binding energy per nucleon for C 12 , N 14 , O 16 C , and comment on their relative magnitudes. Masses ... 931 M e V ) ( Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    19. What is the output of the following? M = [ Mon', 45, Tue', 43, Wed', 42) print(M[-1]) * O a) 1] O b ... 42] O c) [ Mon] O d) [42] Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    There are six tickets numbered from O to 5. Two tickets are selected at random from the lot. What is the ... (c) prime number Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A piece of wood from an archaeological source shows a . 14 C . activity which is 60 % 60 of the activity found in ... C = 5570 . year) Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    8. Which shortcut key do we use to display Document Properties dialog box? * O Ctrl + F3 O Ctrl + J O Ctrl + C O Ctrl + F5 Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    13. Copy command is always followed by:- O a) Cut O b) Delete O c) Paste O d) None Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    On numeric keypad O’ is to be pressed by the right hand_______ finger (a) Thumb (b) Index (c) Ring (d) Little Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
...