Writing text into and retrieving text from the text file using command line auguments - c++

Hello I want to write the output of my C++ Program in a text file using Command Line Argument then afterward retrieve it from the file . can you help me please I cannot get a satisfying code help.

If you are at a unix type command prompt (ie bash or similar), you can generally just redirect your output to a text file using '>'. So if your program is called "MyCommand", just do:
./MyCommand > file.txt
After that, you can use a text editor to open the file, or anything else you'd do with a plain text file.
If you are trying to write out to a file programatically, you need to open up a file handle. There are many ways to do this, one would be to use the standard file handling functions built into the C library. See here for a tutorial:
http://www.tutorialspoint.com/cprogramming/c_file_io.htm

Related

How to read .inp file in c++?

I have a dataset, a ".inp" format file, and I need to read this file in c++. However, the fopen() fread() method seemed to fail and read the wrong data(e.g. the first integer should be 262144, the fread yields an integer much larger than this nevertheless).
To be more specific, my ".inp" file contains a few integers and float points, how can I read them successfully in c++?
enter image description here
This is the screenshot of the "*.inp" file from Notepad++. Basically this is a text file.
I solved it by coping the data into a txt. However, I am still not aware how to read "*.inp"
I found some info about INP file extension. It seems like there are multiple variances of it, each meant to be used for different purpose. Where is your file coming from? As for soultion, if you can't open the file using fopen/fstream normally, you could treat it as binary and read each value in the way you specify. Other than that, I could think of calling system functions to get file contents (like cat in linux for example), then if there are some random characters, you could parse your string to ommit them.
Here is example of how to call cat in C++:
Simple way to call 'cat' from c++?

Is there anyway redirecting (cin) input from txt file in console twice, for C++?

It is rather convenient to redirect input stream from different txt files in CMD console, since I don't need to write a file name and open it in my code. Like typing code.exe < input.txtin windows OS.
However, if I want to redirect two file like input1.txt and input2.txt to two input streams(cin>>) located in two positions of my code. How should I deal with it? I just know how to redirect once rather than multiple times. Any help would be appreciated.
Getting the two inputs in is the simple problem: TYPE file1.txt file2.txt > code.exe. But how would your first std::cin statement know which line is the last line of file1.txt ?
The normal solution is to use code.exe file1.txt file2.txt, and read both files using their own std::ifstream.

Fetch particular pattern from file and read file

I have text file which contain some pattern(First I thought to use awk command into text file
into C++).The below solution works fine for us one single file.
https://stackoverflow.com/questions/15151055/truncate-file-in-linux
But We are getting multiple file which also contain different pattern.So we need to write awk
command for each file which will not be generic solution.
Than after I have found lexer (Flex) for pattern matching in c++ (linux enviroment) But I faced some issue and could write lexer file. So I thought do we have any open source library in linux platform for pattern match in text file and convert into xml file. ( work in progress for google but do not have any concrete solution).
In brief,
1)Search Pattern into Text File(In current, we are using awk command in c++ (in case any general solution)
2)Read Tabular format file into C++.
I hope I am able to convey my message.

Folder with 1300 png files into html images list

I've got folder with about 1300 png icons. What I need is html file with all of them inside like:
<img src="path-to-image.png" alt="file name without .png" id="file-name-without-.png" class="icon"/>
Its easy as hell but with that number of files its pure waste of time to do it manually. Have you any ideas how to automate it?
If you need it just once, then do a "dir" or "ls" and redirect it to a file, then use an editor with macro-ability like notepad++ to record modifying a single line like you desire, then hit play macro for the remainder of the file. If it's dynamic, use PHP.
I would not use C++ to do this. I would use vi, honestly, because running regular expressions repeatedly is all that is needed for this.
But young an do this in C++. I would start with a plan text file with all the file names generated by Dir or ls on the command prompt.
Then write code that takes a line of input and turns it into a line formatted the way you want. Test this and get it working on a single line first.
The RE engine of C++ is probably overkill (and is not all that well supported in compilers), but substr and basic find and replace is all you need. Is there a string library you are familiar with? std::string would do.
To generate the file name without PNG, check the last four characters and see if they exist and are .PNG (if not report an error). Then strip them. To remove dashes, copy characters to a new string but if you are reading a dash write a space. Everything else is just string concatenation.

how to read from a text file contents file name extension in c++?

Hello I want to read from a text file full of directory contents
Here's my example:
below is my text file called MyText.txt
MyText.txt
title.txt,image.png,sound.mp3
I want to be able to read that .txt extension not the filename and I want it to be for file extensions only for example .txt or .mp3 how would I do that in c++?.
When I mean read I mean reference it in a if statement like this:
if(.mp3 exists in a text file)
{
fprintf(stderr,"sees the mp3 extensions");
}
I'm running Windows 7 32-bit.
I need a more cross platform approach.
May I suggest you to read a tutorial on C++ file handling and another one on C++ strings?
There is no a quick solution: you have to read the file using the ifstream class.
After reading the file and storing it in one or more strings, you can then use the find and substr string methods to create a queue of discrete filenames. Using the same methods, you can then split the queued elements again, in order to find the extensions and add them to a set. A set does not allow duplicates, so you are sure all the extensions will appear only once.