eof

eof problem c++

走远了吗. 提交于 2019-12-28 07:02:09
问题 i am using Dev C++ on windows xp #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string STRING; ifstream infile; infile.open ("sample.txt"); while(!infile.eof) { getline(infile,STRING); cout<<STRING; } infile.close(); return 0; } this codes gives the following error C:\C++\read.cpp: In function `int main()': C:\C++\read.cpp:11: error: could not convert `infile.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>

Checking for an empty file in C++

会有一股神秘感。 提交于 2019-12-27 12:17:22
问题 Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it's empty, then you close it right away? Thanks. Edit, I tried using the fseek method, but I get an error saying 'cannot convert ifstream to FILE *'. My function's parameter is myFunction(ifstream &inFile) 回答1: Perhaps something akin to: bool is_empty(std::ifstream& pFile) { return pFile.peek() == std::ifstream::traits_type::eof(); } Short and sweet. With concerns to your error, the

python take input when number of input is not specified

此生再无相见时 提交于 2019-12-25 17:25:24
问题 I am new to python and trying to solve a problem in SPOJ , In this question number of input(maximum 10 is specified, how ever it could be anything between 1 to 10) is not specified hence it gives NZEC error I tried this: t = 10 while(t>0): t = t - 1 n = raw_input() if(len(n) == 0): break but it does not work in c we can use EOF to determine this please help 回答1: Solved Use try: while True: n = int(raw_input()) #do something except: pass 来源: https://stackoverflow.com/questions/38621513/python

Read a file twice in C++ because of eof?

做~自己de王妃 提交于 2019-12-25 10:16:33
问题 I read numbers from a file, apply 3 functions and print out to another file: int main(int argc, char** argv) { std::ifstream fin; fin.open("input.txt"); std::ofstream fout; fout.open("output.txt", std::ios::app); char arr[50]; int a,b; int N;//number to factor while (!fin.eof()){ //Print backward fin >> arr; PrintBackward( arr ); fout << endl; //Greatest common divisor ((fin >> a) >> b); fout << gcd( a, b ); fout << endl; //Find prime factor fin >> N; PrimeFactor(N); fout << endl; } fin.close

fgetwc EOF loop test fails, but 65535 OK

人盡茶涼 提交于 2019-12-25 08:56:57
问题 VS10 & MCBS: For this I have created a file called c:\eoftest containing the text "test" . The value of ch on the 5th pass in the following code is 65535 returned by fgetwc, but it does not equate to EOF, which we all know is defined in stdio.h as (-1): #include <stdio.h> #include <windows.h> int main() { int ch; FILE *stream = NULL; wchar_t buf[5]; memset (buf, '\0', sizeof(buf)); stream = _wfopen(L"C:\\eoftest.txt", L"r"); for (int i = 0; (i < (sizeof(buf) - 1) && ((ch = fgetwc(stream)) !=

Linux Redirection to C program - File Reading

孤人 提交于 2019-12-25 07:48:33
问题 I want to read lines of numbers from the text file ( filename.txt ) using a function in C. How do I open this file (provided the filename is only given through a redirection on Unix)? i.e. ./cfile < filename.txt int main (void) { char filename[20]; fgets(filename, 19, stdin); FILE *fp; fp = fopen(filename, "r"); } So, would this be correct; also, how do I access one line at a time from the file (all I know is EOF has to be used somewhere)? 回答1: The contents of filename.txt gets redirected to

getc(fp) causing trouble

一笑奈何 提交于 2019-12-25 05:10:04
问题 Here is my code. #include<stdlib.h> #include<stdio.h> int main(int argc,char** argv) { char a; a=9; FILE * fp; fp=fopen(argv[1],"r"); while(a!= EOF) { a=fgetc(fp); printf("\n%d",a); } } The output to this is alright but at the end I am getting a weird character with -1 (since I am printing integer value. How to stop it at EOF only? Also what is this character? 回答1: Besides the methods in the other answers, you can also do like this: while ((a = fgetc(fp)) != EOF) { printf("%d\n", a); } Now

Can't write the last part of a txt file to cout using ifstream

半世苍凉 提交于 2019-12-25 02:31:30
问题 The code below will print all of the text from the sample text file I'm using except for the last little snippet of it. I think this has something to do with the eof or the byte size I'm using not working as I expect. #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]){ int length; char* buffer; //get file stream and open local file. ifstream stream; stream.open("SampleFile.txt", ios::binary); stream.seekg(0, ios::end); length = stream.tellg(); stream

sending an error message from SQL to the browser

五迷三道 提交于 2019-12-24 22:07:42
问题 I have a stored procedure for checking business rules in a holiday booking system but I need it to return error messages if it fails so far I have been using if statements and the else(my error is in the else) looks as follows: BEGIN SELECT 'Error1' END im using ASP adodb connection (apprentice in training) and this is how im trying to retrieve it: if not objDBRS.EOF then Session("Error") = objDBRS(0) if Session("Error") = "Error1" Then Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert("

I need a way to read in floats twice in C. Each time ended by <Ctrl>-d

懵懂的女人 提交于 2019-12-24 18:32:20
问题 I need to read in the coefficients (as floats) of a polynomial and mark the end by ctrl-d. Then I need to read in x values and display f(x). Also ending that with ctrl-d. So far I have tried it with the scanf function. Reading the coefficients work well but after typing ctrl-d the first time, scanf will not read in x values. #include <stdio.h> int main(){ int count = 0; float poly[33]; while(scanf("%f", &poly[count]) != EOF){ // reading the coeffs count++; } printf("Bitte Stellen zur