I'm experimenting with pipes in C. I want to connect the Standard Output of file1:
int main() {
printf("6");
}
...to the Standard Input of file2:
int main() {
int number;
scanf("%d", &number);
printf("The number is %d.", number);
}
The output should be: The number is 6. In Xcode, file1 is appended to the executable (the target). I open the Terminal app, change the directory to the Debug folder (which contains the executable file) and issue this line:
.\TestDrive | /usr/someone/somewhere/file2.c
TestDrive is the name of the executable file (that contains file1). Since file2 is not contained in the Debug folder, I have to specify the full path of it. If you wish, you can download the project here.
In the console, I get Permission denied. What am I doing wrong?
If you are piping into /usr/someone/somewhere/file2.c, it is expected that that file is executable; .c files generally are not, but the file that got generated by compiling it probably is.
You should make your TestDrive file executable, from the command line via chmod.
Related
I need to make a game in C for my finals. The user should input the map file he wants to play.
Here's my simple code:
int main(){
FILE *map;
char fileToRead[100];
do{
printf("Insert file name: ");
fgets(fileToRead, 100, stdin);
map = fopen("/Users/rajunior/Desktop/map_2.txt", "r");
//map = fopen(fileToRead, "r");
printf("%s", fileToRead);
If I use the "map = fopen("/Users/rajunior...)" hardcoded, it works!
But I need to use the second (commented) option; the first one is useless for my purpose.
In other words, I need the fileToRead to be in the same directory as my .c, but how?
screenshot: https://imgur.com/a/DbX9tw4
Option 1: Install the command line tools. Put the C file and the text file in the same directory. Open a terminal window. Compile and run from the command line. If I recall correctly, the command line tools download can be found in Preferences.../Downloads.
Option 2: Go to the Product/Scheme/Edit Scheme... menu. When the dialog box appears, select Run at the left and Options at the top. Then look for Working Directory. Set the working directory to point to the directory where the text file is.
This was going to be a comment, but it is too long for comfort.
You'll need to know the current directory of the process when it is run. If you run it from the shell, the current directory of your program will be the same as the current directory of the program. If you run it from within XCode, I've no idea what the directory will be, but it probably won't be where the source is — it'll be in a build directory of some sort, probably.
Your program can find out where it is run from with getcwd(). Then you'll be able to tell how to chdir() to the directory where the source is (as long as the program knows where the source is, because you told it somehow — argument or command line variable, or …). Or you can determine how to create a relative path name that will find the file in the source directory.
There's probably an XCode (maybe Objective-C) way to find the information, perhaps via plists.
I don't code for a Mac; I only code on a Mac, and I run XCode itself rather seldom.
I am trying to get the HDD serial key of a windows PC using system() command and save that number in a text (.txt) file with a file name that user chooses. Everything is working fine before the system() command, but the system() command is NOT changing the file name,that is, instead of naming the file after the user choice, it is just naming it after "contract_file_name" with no extension. For example: if I give file name: blahblah , it's supposed to create a text file with the name "blahblah.txt" (containing HDD serial Key), but instead it's creating a file with "contract_file_name".
Here is code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char contract_file_name[100];
FILE *fp1 ;
filename:
printf("Please Give an APPROPRIATE name for SENDER-RECEIVER CONTRACT file. Please Don't use any .txt extension\nFILE NAME: ");
gets(contract_file_name);
strcat(contract_file_name,".txt");
if((fp1=fopen(contract_file_name,"r")))
{
printf("A Contract File with %s already EXITS.Please Choose another name\n".contract_file_name);
goto filename;
}
else
{
fp1= fopen(contract_file_name,"w");
fprintf(fp1,"$Sender: %s\n",getenv("USERNAME"));
fclose(fp1);
system("wmic path win32_physicalmedia get SerialNumber >> contract_file_name"); //Having problem in this line,I think.
fp1 = fopen("contract_file_name","a");
fprintf(fp1,"\n");
fclose(fp1);
}
return 0;
}
I can feel that the problem is with my method system() command, But can't find any solution.Can anyone please suggest me how to FIX this problem?
Thanks in Advance.
The problem is not with the system function (which you shouldn't be using for this task anyway), but with your misunderstanding how string substitution works.
You have a variable contract_file_name of type "array of 100 char" and apparently expect every occurance of "contract_file_name" inside a string to automatically being replaced. This is called "variable expansion" and is in fact supported in some languages. Most notably shell and Perl. However you actually have to mark the variables in a string for replacement (usually by prepending a $ sign). That does not work in C! C is a very frugal language and doesn't come with that feature.
Anyway your program immediately reads back the file and hence using a fixed filename or a filename at all is a bad idea anyway (think about what might happen if several instances of your program get run at the same time; can you prove that all parts in play are idempotent? probably not).
Instead you should run wmic with popen. With popen the output of wmic is written to a FIFO which you can read directy as if it were a file, without a file ever being written to a disk: https://msdn.microsoft.com/en-us/library/96ayss4b.aspx
You have contract_file_name is a literal string in your system call. You have to put the value of the variable in the string first, something like
char tmp[100+50];
sprintf(tmp, "wmic path win32_physicalmedia get SerialNumber >> %s", contract_file_name);
system(tmp);
You should also limit what you read into contract_file_name to 99 characters.
I have written a simple C code in visual C++
I am planning to open a text file for reading , but whenever i enter path it shows "unable to open the file".
then i hardcoded the path in the program itself. still same error , can anyone tell me what I am doing worng ? or where I wil have to copy paste that file so that i can open it in visual c++ through my code ?
here is my code :
#include<stdio.h>
#include<conio.h>
int main(){
FILE *p;
char file1[20];
char ch,i;
printf("\nEnter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r"); // I have tried changing it with actual path to the file
if(p==NULL)
{
printf("cannot open %s",file1);getch();
exit(0);
}
while((i=getc(q))!=EOF)
printf("%c",i);
fclose(p);
return 0;
}
Save one file in your directory where you have saved the program.
And try it typing the perfect full name of the file including format of the file.
Your code seems error free.
When you're telling directory during execution use // only dont use / otherwise it will show you error and file won't open.
it worked after Entering double back slash "\\" instead of single back slash after the Drive name (example "c:\\") for the path to the file.
I am working with command prompt features with a simple program to generate a text file in C. Here is the program:
#include <stdio.h>
int main(void)
{
char buf[80];
fgets(buf, 30, stdin);
printf("the input was %s\n", buf);
return 0;
}
My programming book is wanting to show how to play with the command prompt to make text files from programs, and instructs typing the word 'redirect' followed by '>' then the name of the program name with '.txt'. as below:
redirect> programname.txt
Now this IS generating a file 'programname.txt' on the desktop, but it is empty. The book purports that recipe should allow me to enter a string (as the program is DESIGNED to do) and that this string will be inside a generated programname.txt file. Also, there is a warning in the command line: "not recognized as an internal or external command". I've had this schpill before, but the text file generation did WORK, in that it did generate the .txt file. What am I missing here, for this program to work as intended?
You seem to be confused by the fact that it is not your program, but the shell which creates the file programname.txt, before it even tries to run your program.
And after the first succeeded and created an empty file, the latter probably fails because there is no command redirect in your PATH or such a thing exists as a builtin in your shell, as has already been suggested.
The usual way to perform output redirection in a shell is to use the > filename, but not with redirect before it contrary to what you say. The thing that comes before the > is the command to be redirected.
So, let's say you compile your program and save it as foo in the current directory (e.g. cc -o foo myprogram.c). In that case, you can redirect its output by saying:
./foo > filename.txt
I have to write code in C to extract a password protected rar file in windows. I don't have any clue about how to do this. can anybody suggest me something or provide a sample piece of code? I will be very thankful.
EDIT:
This is the code I am using to open the rar file.In the system command ranjit is the password. It's giving the error undefined symbol_system in module+thefile name. Can anybody help me?? I am struggling on this since two days.
EDIT: This code opens the archive but do not extract it. If I uses the unrar command in command line, it extracts the file. What I should I do?
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
char file[20];
char file2[50] = "F:\\Program Files\\WinRAR\\unrar.exe";
printf("enter the name of the rar file : ");
gets(file);
puts(file);
system(("%s e -p ranjit %s >C:\stdout.log 2>C:\stderr.log",file2, file));
getchar();
return 0;
}
In addition to what karlphilip's suggestions there's also a couple of potentialliy interesting looking resources at http://www.rarlabs.com/rar_add.htm.
In particular I am thinking UnRAR.dll and UnRAR source may be relevant. I can't really check it out at the momment though.
Using unrar library - extracting files into a filestream buffer
But if you're looking for a pure C solution, take a look at: http://www.unrarlib.org/
Quote from their FAQ: The URARFileLib (short name for UniquE RAR File Library, also called unrarlib) is a free library for C programmers to access RAR archives.
Another approach, which I just tested successfully, doesn't require the use of external libraries to decompress rar files. Use system() to invoke a command-line tool (such as unrar ) already installed on your system to do the job:
system("unrar x -ppassword protected_file.rar /destination_directory");
For instance, let's say the protected file was named file.rar, the password was 1234 and the destination directory was /home/user, you would call system() with the following parameters:
system("unrar x -p1234 file.rar /home/user/");