Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have studied this for about 3 years and I have not found anywhere in books or on the internet that tells me how to do this. Never.

I am using codeblocks 17.12 with gcc 5.1 set for UTF-8 on Windows.

What I have tried:

Everying that I thought of. NONE of it worked.

I have spent hours reading comments on SO. I studied cplusplus.com a LOT. I studied tutorialspoint.com a LOT. I studied cppreference.com over and over again. I learned a lot, but not how to do this one thing.

My compiler: "error". "error". "error". "[Ha ha ha, another]error".


I want to be able to read the first byte from a file and then get the bits of that byte.

I am NOT a professional programmer. You are. HELP ! Please.
Posted
Updated 27-Jul-22 19:16pm
Comments
Dave Kreskowiak 26-Jul-22 0:34am    
OK, so you';ve bitched that you cannot figure out how to read a file.

You have not said anything about the errors you're getting nor posted the code you're using.

Reposting this again and again will not get you different answers.
Member 15078716 27-Jul-22 23:40pm    
It might. But you did exceptionally well answering on that other page. https://www.codeproject.com/Answers/5337680/What-is-this-std-istreambuf-iterator-conversion-pr#answer1 and I thank you for it.

Again thank you. I was feeling desperate. No need, but I had asked so many times in so many other ways and received no answer that my system and compiler worked with. Now I have Thank you.
merano99 26-Jul-22 1:41am    
In fact, there are many ways to read a byte from a file under Windows with C or C++. Actually, they should all work if the file is readable and not empty. Please list what did not work and what the error was.
Member 15078716 27-Jul-22 23:45pm    
You are SO RIGHT ! "If the file is readable and not empty." I checked the file and found that even though I wrote it and checked it a while ago, it was now empty. I had been clearing it in my testing. All the code in the world could not get "hello world" out of "". Thank you. If you had supplied this as a solution, I would have accepted it and given you a 5. Thank you.

merano99 28-Jul-22 1:12am    
I'm glad that my comment finally led to the solution. As suggested I create a solution.

Quote:
My compiler: "error". "error". "error". "[Ha ha ha, another]error".

First off, if you get compilation errors, then that is to be expected - but we can't help fix them without seeing the code and the error message(s) any more than a mechanic can fix your car if you don't let him near it with a spanner!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

Quote:
I have studied this for about 3 years and I have not found anywhere in books or on the internet that tells me how to do this. Never.

Reading a byte from a file is trivial in most languages - let's take C as it's the most basic ...
1) Create a variable to refer to the file:
C
FILE *fp;
2) Open the file:
C
fp = fopen("C:\\MyFolder\\MyFile.bin", "r");
3) Create a buffer to read the byte into:
C
unsigned char data;
4) Read a byte:
C
fread(&data, 1, 1, fp);
5) Close the file:
C
fclose(fp);

The process is very similar in all languages: Open, read, close with appropriate variables to store data in.

Here is the whole program in C:
C
#include <stdio.h>

int main()
    {
    printf("Hello World\n");
    FILE *fp;
    fp = fopen("C:\\MyFolder\\MyFile.bin", "r");
    unsigned char data;
    printf("\"%c\"\n", data);
    fread(&data, 1, 1, fp);
    fclose(fp);
    printf("I read one byte from the file.\nIt was \"%c\"\n", data);
    return 0;
    }
Inthe real world, we'd add code to check the file existed, and that it contained enough data to actually read, but that is an exercise for the reader!
 
Share this answer
 
Comments
CPallini 26-Jul-22 2:36am    
5.
Member 15078716 27-Jul-22 23:48pm    
Multiple places and multiple correct anwers, I have not even gotten to your answer when I found on https://www.codeproject.com/Answers/5337680/What-is-this-std-istreambuf-iterator-conversion-pr#answer1 two acceptable answers. Without even testing your's I expect it to be correct, and I am moving on to other things already. You get accepted and a 5. Thank you.
Or in C++ you could use the get function of ifstream: std::basic_istream<CharT,Traits>::read - cppreference.com[^].

Quote:
I have studied this for about 3 years and I have not found anywhere in books or on the internet that tells me how to do this. Never.
Three years and you did not think of studying the documentation of either language?
 
Share this answer
 
Comments
Member 15078716 27-Jul-22 23:52pm    
Richard ! You should know better than that by now. Shame on you for even thinking such a thing. You still get accepted and a 5 on the other page.
In fact, there are many ways to read a byte from a file under Windows with C or C++. Actually, they should all work if the file is readable and not empty. Most system or library calls have return values ​​or return the number of data read or written as a parameter. It is always advisable to evaluate them.
When dealing with files, it can also help to determine the file status with fstat() or lstat().
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900