Detecting EOF in C

EOF is just a macro with a value (usually -1). You have to test something against EOF, such as the result of a getchar() call. One way to test for the end of a stream is with the feof function. if (feof(stdin)) Note, that the ‘end of stream’ state will only be set after a … Read more

How can I get an int from stdio in C?

I’m not fully sure that this is what you’re looking for, but if your question is how to read an integer using <stdio.h>, then the proper syntax is int myInt; scanf(“%d”, &myInt); You’ll need to do a lot of error-handling to ensure that this works correctly, of course, but this should be a good start. … Read more

Is there a Windows equivalent to fdopen for HANDLEs?

Unfortunately, HANDLEs are completely different beasts from FILE*s and file descriptors. The CRT ultimately handles files in terms of HANDLEs and associates those HANDLEs to a file descriptor. Those file descriptors in turn backs the structure pointer by FILE*. Fortunately, there is a section on this MSDN page that describes functions that “provide a way … Read more

Find all substring’s occurrences and locations

string str,sub; // str is string to search, sub is the substring to search for vector<size_t> positions; // holds all the positions that sub occurs within str size_t pos = str.find(sub, 0); while(pos != string::npos) { positions.push_back(pos); pos = str.find(sub,pos+1); } Edit I misread your post, you said substring, and I assumed you meant you … Read more

What exactly is the FILE keyword in C?

is this a keyword or special data type for C to handle files with? What you are refering to is a typedef’d structure used by the standard io library to hold the appropriate data for use of fopen, and its family of functions. Why is it defined as a pointer? With a pointer to a … Read more