What cmd can I use to find all (text) files whose name is of some form in a directory? - bash

I would like to write a bash script that runs a .cpp program on a number of text files (I'll henceforth refer to those text files as input files). The .cpp program assumes that there is an input text file in the same directory level called "input.txt". However, the inputs I would like to run through the program are in a sub-directory and there are more than one input txt files that I would like to run through the program.
The input txt files are named using the following convention:
input1.txt, input2.txt, input3.txt, etc,...
They are placed in a folder called "myInputs" which contains text files with similar name e.g., input1.txt.Sol that I'm not interested in.
The commands I would like to have in my script should look in 'myInputs' for txt files of the form: input%.txt where % may be a number. For each file of that form, the script should print out the name of the file on a line-by-line basis. i.e., input1.txt should be on its own line, input2.txt should be on it own line.
Any tip is appreciated!

There is a bash command called find
http://unixhelp.ed.ac.uk/CGI/man-cgi?find
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
(void)system( "find \"MyInputs\" \"input*.txt\"" );
/* whatever comes next */
return 0;
}
Unfortunately, I don't know that much C++, but here's a snippet in C

Related

Opening a file from a path relative to the binary when running from a different directory

Lets say I have a binary, sayhello along with a text file hello.txt in the directory /path/to/hello/. The binary reads the text file and outputs its contents to stdout:
#include <stdio.h>
int main()
{
char buffer[14];
FILE* file = fopen("/path/to/hello/hello.txt", "r");
fgets(buffer, sizeof(buffer), file);
puts(buffer);
fclose(file);
return 0;
}
[/path/to/hello/] $ ./sayhello Hello, world!
I can run sayhello from anywhere, if I add its directory to PATH:
export PATH=$PATH:/path/to/hello/
[/other/path/] $ sayhello Hello, world!
I know I can use fopen with a relative path as well: FILE* file = fopen("hello.txt", "r"); However, if I did this, I would always have to run sayhello from the directory that contains the text file (and in my case, the binary as well.) I would like fopen to open the file from a path that's relative to the binary itself, not the directory where I happen to run it from. This would let me move the directory with the files somewhere else and just modify my PATH accordingly, leaving the binary itself unmodified. Is there an easy way to achieve this?

How can we use .txt file as command line input in c++ language on a windows machine?

I want to make my c++ file read input from one text file and save output in another text file. This is to check my competitive programming questions.
I think this solution works for you.
#include <iostream>
#include <fstream>
//#define cin fin
//#define cout fout
using namespace std;
int main(){
//ofstream fout("output.txt");
//ifstream fin("input.txt");
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
}
return 0;
}
If you want to use as standard input output then you can use as it is. As per your problem you can use it by only removing two slashes(comments).
In the above program "input.txt" is file name for taking input an "output.txt" is the file in which output will be stored.
Note: You only have to provide "input.txt" the program will automatically generate "output.txt".
You can rename "input.txt" and "output.txt" as per your requirement.
If you are not satisfied with the above solution then you can use a program named hightail. Hightail is an automatic tester for programming contest. To know more about hightail and its working click here.
Unclear of what you are asking. Since your question and your description seems asking two different situations.
For your question, you seems to ask how to input a command (like dir or systeminfo) from a file. You can use the function system() in the header file stdlib.h to execute the command read from that file.
For your description, you seems to ask how to input some data from a file to stdin and output to another file, you can look for command redirection, for example Redirect Windows cmd stdout and stderr to a single file.

Code for searching a word into some text files stops right before first .rtf file

I'm trying to make a program which finds a word into some .rtf files
e.g : I have some text edit files with all the words which starts with all the characters(WordsWhichStartsWithA.rtf, WordsWhichStartsWithB.rtf, WordsWhichStartsWithC.rtf etc), and when I give a word(e.g : "help") I want to return the file where the given word is (in this example "help" will be in WordsWhichStartsWithH.rtf file, and the program will return WordsWhichStartsWithH.rtf NOT WordsWhichStartsWithW.rtf where "whelp" is, I want to find just the word, not the strings which contains the given word)
Here is what I have so far :
import os
for fname in os.listdir('/Users/andreivaran/Desktop/Info'):
if os.path.isfile(fname):
#print fname
with open(fname) as f:
for line in f:
if 'help' in line:
print fname
break
I've printed the fname before the first if checks if the path is file to find out if the code is checking all the text files and I get this
>>>.DS_Store
WordsWhichStartsWithA.rtf
It's returning that .DS_Store which I can't even see in the folder and it's not checking all the .rtf files, it stops after the first one!
Forgot to mention that the directory where the .rtf files are, is different from the directory where the python file is.
Thank you !

Where do I set the Xcode IDE property to read a data file to avoid No such file or directory

This is not a code question but an Xcode IDE question.
For those who pride themselves in code analysis, that's good, but in this case, please try to focus on the question in the Title above not the code.
In the Xcode IDE I created a new project and selected a "Command Line Tool" creating ReadAFile.
I then entered some simple code into main.c to read and display the contents of "Data.txt".
I then selected File, New, File, and selected Empty (For an empty document) and named it Data.txt and then I typed out a sentence then saved the file.
In Xcode, when I choose Product, Run, I get the following error:
Error while opening the file.
: No such file or directory
Program ended with exit code: 1
Where do I set the Xcode IDE property so that it knows where my data file is to avoid getting the "No such file or directory" error?
When I enter the very same code into ReadAFile.c and I type out the very same file "Data.txt" at a terminal prompt using vim, save it, and run make ReadAFile, the code executes properly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char ch, file_name[25];
FILE *fp;
strcpy(file_name,"Data.txt");
fp = fopen(file_name,"r");
if( fp == NULL ) {
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
That's a good question... I have a temporary, work around, answer...
I do respect your request to focusing on the question rather than your code.
As a quick temporary answer, the only answer I have for you at this point is to modify your code, for now, if you are only debugging your code along with your data file. I know hard coding your file locations is a no-no but for debugging purposes the following will get you over this bump in your road. You can try to hard code the directory location along with your data file so that your code can find it. For example, let's say that your Users directory on your Mac is jerryletter, you could copy your Data.txt file to your Users directory ~/jerryletter. Then could modify just one line of your code like the following:
"strcpy(file_name,"/Users/jerryletter/Data.txt");"
DougPan.com

How do I make my `OutputDebugString` messages be clickable links in Visual Studio output window?

I output log/warning/error messages by OutputDebugString() so they are readable in "output" window of Visual Studio.
I want to make those messages to be links. After clicking they will open predefined source file and put a cursor on predefined line, much like this happens with compiler errors.
How would I do this?
Format your output string in a special way:
file_path(line): message
where file_path is a full absolute path of a file to open by click, line is a number of line cursor to be put on and 'message` is anything else.
Visual Studio parses such a string when you double-click it and opens the file.
Example with C++ and boost::format:
#include <windows.h>
#include <boost/format.hpp>
#include <string>
int main()
{
std::string errMsg = "Yay! Fancy link!";
std::string formatted =(boost::format("%s(%i): in function \"%s\": %s\n\n")
% __FILE__ % __LINE__ % __FUNCTION__ % errMsg ).str();
OutputDebugStringA(formatted.c_str());
}

Resources