is your C code supposed to work with a c++ compiler?
Because I have exactly the same code and the abovementioned error keeps showing.
I used the same C++ compiler for both, though I did save the pure C version with the file extension of ".c" rather than ".cpp". Usually the compiler automatically chooses to compile as C that way.
Is there a way to implement this line: FILE* fp = fopen(file_name, "rb");
in your c++ code?
You would need the C header file #include <cstdio> . Your original code didn't have that line.
I can see that you wrote both programs within a function. Is it necessary?
Every program in C or C++ must always have a main() function. That is where execution begins. Any other functions are optional, but they often help to keep things manageable.
For some reasons I would like all the lines to be "global".
You can declare variables such as file_name or res to be global. However, usually global variables are kept to a minimum, it helps avoid unexpected interactions between different parts of the code.
That's definitely a non-starter; to C++, that is just a string with a * in it.
You would need to iterate over all the files in the folder. That is, however, a pretty simple thing to do. We just create a "directory_iterator" pointed at the directory, and use it to get each entry in that directory in turn. Something like this would do it:
1 2 3 4 5 6 7 8 9
for (constauto& entry : fs::directory_iterator(filesystem::path("/home/Desktop/pics/")))
{
// HERE, entry is an entry from that directory
if ((entry.is_regular_file()) // just files, please. Not other directories
{
constauto filenameString = entry.path().filename().string();
// filenameString is now the name of the file, to be checked
// check that it ends with jpeg or some some, and then just do exactly what you do with a single file
There is, but until C++17, C++ had no conception of any kind of underlying filesystem whatsoever; all such functionality is instead provided by the operating system on which it runs (which is, of course, still the case; C++17 simply added a layer of abstraction with the <filesystem> library calling on the OS for you).
Your options are to use the OS API directly, or to identify a third-party library which would do that for you (and leave you to call on that thrid-party library).
If you identify your OS, you will be able to look up the API and see what headers and what functions are provided.
if you need a simple fix to get it done, call dir or ls with the correct parameters eg "dir /b c:\foo\*.jpg > file.txt" and read that file for the list of files to open. If that is too crude for you, you can use the APIs etc. Alternately you can handle 1 file in your program and call it with for each on all the files.
OS libraries and the crude code I gave, none require filesystem. I also lack filesystem; it seems to not be fully supported. I recommend, if you are doing something serious, getting a compiler that supports it. Its cleaner. Window's microsoft gui tools have a nice folder iteration set of tools.
I mistakenly wrote "filestream" but you correctly guessed that I meant "filesystem".
Of course I know that filesystem is an header, but since I'm having an hard time installing it, I asked you for another way without it.
My operative system is Linux, but I am quite unfamiliar with the concept of API.
@ jonnin
Since I am on linux, does it make a difference?
if your linux does not support M$ dir commands (some have scripts to make ls act like dir to be cross platform friendly) then you need ls -A1 *.jpg or whatever instead of dir \b
** you really should have been able to figure that out; linux is hard to use without knowing the basic console commands. I had to look up the naked format option, but you should have known ls - something would do it and gotten it from there...
and, I came in late and missed your goal. if you just want the file sizes, instead of making a list and getting each one, make a list with the size on it.
that would be something like this (there are many ways to do it in unix, none of them clean)
C:\c>ls x -lt | cut -d" " -f6-
21 Sep 27 15:51 x
(x is 21 bytes here). you would have to dig out the size with a simple parse of the line or play with the command to get it down to name and size only.
at some point my approach will have you give in and use the library though, if you want anything the OS can't hand you on a platter it starts becoming too much work.
I am very new to linux, therefore I have to start somewhere.
If I already knew these things, I wouldn't be asking, would I?
That is fair. Again, I encourage you in the strongest way possible, if this is the case, to put the c++ down for a couple of days and get familiar with the unix command prompt. You don't need to know every tool it offers, but you should be able to do a few things...
- copy a file (cp)
- list the files (ls)
- search files for things (grep)
- compile a program by hand (g++ with options)
- execute your program (you may need ./programname to do this, you can hack the ./ away in .profile for your account)
- redirect your program's output and input to/from text files (program > file and program < file)
- make a folder
- delete a file (rm)
- make a simple shell script, eg to compile a program using g++ and options...
- chmod a file (so you can make the above script executable and give yourself read permissions on things etc)
- pipe one command into another to string together several tools at once
- check out the other stuff like awk, just poke around see what seems useful to know
Just the above will make working with linux and c++ much more tolerable for you. You can do most of that with their guis but not all of it, and I can't stress enough how powerful basic competence at the console is and how much more you can do quickly with a few tools.
You compiler may already be c++17 capable. g++ and options...
g++ filename.cpp filename2.cpp ... -Wall -Wextra -std=c++17 -pedantic-errors -O3 -s
I was not trying to be mean. But, my opinion only, if you do not even know ls console command, you have major gaps in your skills that will make trying to use linux like pulling teeth. Tend to this gap, and the c++ efforts will become much easier -- you will see :)
All I have at work is a toy compiler and notepad. You can do a ton with very little.
@jonnin
thank you.
As I mentioned, I started really recently with linux, so I haven't had a chance yet to explore the terminal in its full potential, although I use it to install and run programs/libraries and I can do a few small things like searching for files and so on. As right now, I only need the terminal to run the programs.
Thanks for the tips.
@everybody:
after using dirent.h to get access to a specific directory, I wanted to know what type of variable did I get.
The outcome is "A256_c", which is something that I cannot find anywhere online.
Can anyone tell me what kind of variable this is?