Load file into input stream and use scanf() - c++

I am very new to c++.
I read some code samples of some experienced c++ coders, and noticed that they ran a file starting with scanf, that read through a large input.
However, they didn't use any specified ifstream to read in a file, they seemed to just run scanf as if this entire input .txt file was already in the stream.
Is this because they are using some special IDE where you can load in input file into the standard stream?
I am simply trying to run their code to duplicate something they are doing, but I need some way to read in a 5000+ line text file and then run scanf on it.
How do I do this. I'm not using any sort of IDE, just notepad++. If someone recommends an IDE, i'll take it, but otherwise would just like advice on how to do this. Thanks.

For a start, if they're still using scanf(), they are not experienced C++ programmers. If they were experienced, they would be using the C++ I/O facilities rather than the legacy C ones.
Having said that, you can quite easily use the functions accepting standard input (either scanf() or cin) if you can do redirection on the program, with lines like:
myProg <myInputFile.txt
someOtherProgWhichProducesOutput | myProg
This "attaches" the file myInputFile.txt to the standard input of the myProg program, or takes the output from another program and feeds it to the input of myProg, and is what makes UNIX and its brethren (including Windows in this context, since it too has redirection and pipelines) so powerful, allowing you to string together complicated pipelines from relatively simple commands.

Related

Online CPP Compiler that allows writing/reading

I am trying to write out data and read it back in on ideone.com. I could not figure it out:
ofstream os ("data.dat", ios::binary);
did not work, when I try to read back in with
ifstream is("data.dat", ios::binary);
the size of the data read is 0.
Is there a different syntax at online compilers, such as ideone.com?
Most if not all online compilers that I have seen do not allow you to read and write to files. Most allow you to input text into an input box and then they redirect the standard input to that box so you can change the input to the program from run to run.
Reading this answer by Shafik Yaghmour and the attached documentation it looks like you can create a file with coliru and then use that file in a subsequent program but I have no idea how well that will work with a binary file and it looks like it might be read only.
I would think most services do want to allow file uploads as then people could upload to there service and then the site could be hosting some undesirable content.
If you need to work with files I suggest you use an actual compiler on your PC. If you need something that is free then you can start at Bjarne Stroustrup's An incomplete list of C++ compilers

How do I input and output various file types in c++

I've seen a lot of examples of i/o with text files I'm just wondering if you can do the same with other file types like mp3's, jpg's, zip files, etc..?
Will iostream and fstream work for all of these or do I need another library? Do I need a new sdk?
It's all binary data so I'd think it would be that simple. But I've been unpleasently surprised before.
Could I convert all files to text or binary?
It depend on what you mean by "work"
You can think of those files as a book written in Greek.
If you want to just mess with binary representation (display text in Greek on screen) then yes, you can do that.
If you want to actually extract some info: edit video stream, remove voice from audio (actually understand what is written), then you would need to either parse file format yourself (learn Greek) or use some specialized library (hire a translator).
Either way, filestreams are suited to actually access those files data (and many libraries do use them under the hood)
You can work on binary streams by opening them with openmode binary :
ifstream ifs("mydata.mp3", ios_base::binary);
Then you read and write any binary content. However, if you need to generate or modify such content, play a video or display a piture, the you you need to know the inner details of the format you are using. This can be exremely complex, so a library would be recomended. And even with a library, advanced programming skills are required.
Examples of open source libraries: ffmpeg for usual audio/video format, portaudio for audio, CImg for image processing (in C++), libpng for png graphic format, lipjpeg for jpeg. Note that most libraries offer a C api.
Some OS also supports some native file types (example, windows bitmaps).
You can open these files using fstream, but the important thing to note is you must be intricately aware of what is contained within the file in order to process it.
If you just want to open it and spit out junk, then you can definitely just start at the first line of the file and exhaustively push all data into your console.
If you know what the file looks like on the inside, then you can process it just as you would any other file.
There may be specific libraries for processing specific files, but the fstream library will allow you to access any file you'd like.
All files are just bytes. There's nothing stopping you from reading/writing those bytes however you see fit.
The trick is doing something useful with those bytes. You could read the bytes from a .jpg file, for example, but you have to know what those bytes mean, and that's complicated. Usually it's best to use libraries written by people who know about the format in question, and let them deal with that complexity.

Use User Input/Text File as Code

Is it possible to treat user input as lines of code in C++?
Or write it to a text file, then run that as c++ code?
I'm writing my own version of matlab, and right now it can do a lot of the numerical stuff, but only if I write the commands in like a script in main. Is there a way I could do it like matlab's command line?
Yes, there are many libraries that allow for scripting like Lua or Chaiscript which will allow you to take a file of code and execute it. There's also the advantage of being able to make changes to the code and not having to recompile each time.

run program with perl or C++

I am looking for a way to grade C++ programs.
I want to be able to execute numerous programs that will have a standard input/output path, so each program will be required to output and input information at the appropriate time.
What is the best way to do this? I have seen that some people say write a C++ program that calls fork() but my question about that is how do you interact with the same input and output screen? (in other words, once you fork that process, can you feed that process input and view it's output?) Would a perl script work better?
If so, can you give me a few lines of the perl script and what it would look like.
You should use the Posix popen or open3 to open the program and interract with its stdin and stdout. You should be able to do this in any language; personally I would use Ruby and rspec to do the grading.
http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html
Be sure to run the students' code as a user with restricted permissions and use a chroot jail!

C++ Passing Options To Executable

How do you pass options to an executable? Is there an easier way than making the options boolean arguments?
EDIT: The last two answers have suggested using arguments. I know I can code a workable solution like that, but I'd rather have them be options.
EDIT2: Per requests for clarification, I'll use this simple example:
It's fairly easy to handle arguments because they automatically get parsed into an array.
./printfile file.txt 1000
If I want to know what the name of the file the user wants to print, I access it via argv[1].
Now about how this situation:
./printfile file.txt 1000 --nolinebreaks
The user wants to print the file with no line breaks. This is not required for the program to be able to run (as the filename and number of lines to print are), but the user has the option of using if if s/he would like. Now I could do this using:
./printfile file.txt 1000 true
The usage prompt would inform the user that the third argument is used to determine whether to print the file with line breaks or not. However, this seems rather clumsy.
Command-line arguments is the way to go. You may want to consider using Boost.ProgramOptions to simplify this task.
You seem to think that there is some fundamental difference between "options" that start with "--" and "arguments" that don't. The only difference is in how you parse them.
It might be worth your time to look at GNU's getopt()/getopt_long() option parser. It supports passing arguments with options such as --number-of-line-breaks 47.
I use two methods for passing information:
1/ The use of command line arguments, which are made easier to handle with specific libraries such as getargs.
2/ As environment variables, using getenv.
Pax has the right idea here.
If you need more thorough two-way communication, open the process with pipes and send stuff to stdin/listen on stdout.
You can also use Window's PostMessage() function. This is very handy if the executable you want to send the options to is already running. I can post some example code if you are interested in this technique.
The question isn't blazingly clear as to the context and just what you are trying to do - you mean running an executable from within a C++ program? There are several standard C library functions with names like execl(), execv(), execve(), ... that take the options as strings or pointer to an array of strings. There's also system() which takes a string containing whatever you'd be typing at a bash prompt, options and all.
I like the popt library. It is C, but works fine from C++ as well.
It doesn't appear to be cross-platform though. I found that out when I had to hack out my own API-compatible version of it for a Windows port of some Linux software.
You can put options in a .ini file and use the GetPrivateProfileXXX API's to create a class that can read the type of program options you're looking for from the .ini.
You can also create an interactive shell for your app to change certain settings real-time.
EDIT:
From your edits, can't you just parse each option looking for special keywords associated with that option that are "optional"?