I have been creating a C program that is going to pull a range of data from excel and store it in variables. My question, how do i create a directory in C that uses variables. I have tried several ways yesterday while at work.
#include <stdio.h>
int main(void)
{
//creating directory (works)
system("md E:\sub_directory");
system("mkdir E:\\sub_dir2");
//trying to create directory based off variables (not working yet)
char CustName[50] = "Mars";
char Product[50] = "Chocolate Drops";
int JobNumber = 100;
system("md E:\\",JobNumber,CustName,Product"",JobNumber,CustName,Product);
system("md E:\\%04i %s %s",JobNumber,CustName,Product);
return 0;
}
As you can see above, this is the code that i finished up with. The first section was just a reference point so i know base command structure. I then proceeded to fiddle with formatting that i currently know which didnt work.
Would anyone be able to shed some light on how i would use variables name to create the directories? (im currently working on a windows 10 machine but the program is going to be run on a windows 7 machine. I dont know if this changes anything.
You should use sprintf to create one string with the complete pathname you want to create. Then you should use the mkdir function of the standard library to create the directory.
Note that to create a directory, all component paths must exist, so to create E:\my\path you may first need to create E:\my and then E:\my\path.
I have had a look at sprintf and i think i understand it. i have the following code:
char CustName[50] = "Mars";
char Product[50] = "Chocolate Drops";
int JobNumber = 100;
char *filepath[100];
sprintf(*filepath, "mkdir D:\\%04i %s %s", JobNumber, CustName, Product);
puts(*filepath);
sorry if this isnt the correct use, i am still pritty new to programming
Related
So I have an assignment for hs and honestly I do not know how to program in C so I would appreciate all the help I can get.
Anyways my problem is that I cannot for the life of me read a content of a file, asign it to a variable and compare it to another variable if the content is the same.
I am making a login interface with already pre-input admin email and admin password and here is my mess of a code for now:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *emailf;
emailf = fopen("email.txt", "r");
char* fgets(char* emailx){
return emailx;
}
fclose(emailf);
char email [30];
char password [16];
do{
printf("E-mail:");
scanf("%c", &email);
}
while (email != emailx);
What it does is tells me that emailx variable is not even declared so if you could help me I would be glad! And also it does not have to resemble this code since I do not know jack crap about C programming language so if you could tell me a different way to try what I am trying would be good as well. Thank you!
There are several things that are not good in your program.
Firstable, are you sure you are openning properly the file "email.txt" ?
Then, this part of the code is wrong, you cannot define a function inside a function, especially if this function already exist in a library:
char* fgets(char* emailx){
return emailx;
}
maybe you should try something like
emailx = fgets(emailx);
But first you should declare your variable.
I ve never used those functions so I don t know how it works precisely, but chek the manual.
I am using Xcode for c programming. I have a project called system_commands. Inside this project I have a file A, and a file B.
This is file A:
int main(int argc, char *argvector[])
{
char *arguments[] = {"./runProcesses","Hello","World",NULL};
execvp("./runProcesses", arguments);
return 0;
}
File A simply "executes" File B, and file B is here:
int main(int argc, char *argvector[])
{
for(int i = 0; i < argc; i++)
{
printf("%s", argvector[i]);
}
//some forks etc
}
Obviously, Xcode will not allow this, since there are two main methods inside the project. But is there a workaround somehow? Do I really have to create another project, so that the files reside in different projects?
Or is there a way I can execvp() a file without main method?
If you don't want two projects, you can create a second target ("File" - "New" - "Target..." and choose another "Console app") for your second program. Put your other main.c there:
You can also go to the Target settings for the first console app and add a dependency for the second console app, that way, it will build the second, if needed, whenever you run the first one.
Frankly, this multiple target approach is designed when you have an app with some shared code base, but it can be used in this scenario, too.
The other approach is to go ahead and have multiple projects, but work with them under a single "workspace". So, create a folder, create your two projects in that folder, and then create a "Workspace" (which I called "MyProject", in my example below) and add the two xcodeproj files for the two separate projects ("FirstApp" and "SecondApp" in my example below).
So, in Xcode it looks like:
And in the file system, it looks like:
So, in this case, you have two projects, but you can work with them together under a single workspace, making it really easy to work with both projects at the same time. Just open that xcworkspace workspace rather than the individual xcodeproj files.
I know this sounds like a dumb question, but I'm new to programming and I just want to place my program into the start up folder whenever someone runs it.
For example: End user runs my application --> My application copies itself into their start up folder (or moves itself)
I'm using C to write this application, and it would be great if you guys can refer me to a function that will allow me to do this (preferably a WinAPI function, but it doesn't really matter).
Thank you, all help is appreciated.
I would probably write the essential parts of this into a separate function, but here are the basics:
Using Windows (per WinAPI reference in post)...
#include <ansi_c.h>
#include <windows.h>
int main(void)
{
char filename[ MAX_PATH ];
char newLocation[]="C:\\enterstartupdirhere";//put actual path here (i.e. don't use as is)
BOOL stats=0;
DWORD size = GetModuleFileNameA( NULL, filename, MAX_PATH );
if (size)
CopyFile(filename, newLocation, stats);
else
printf("Could not find EXE file name.\n");
return 0;
}
So I am tasked with creating a shell. I have the functions working correctly (e.g. dir, clear, quit, etc.), but I have a question about the 'dir' function. Currently 'dir' works fine. It lists the files of the directory that the program is located in. What I want to do is list the directory of another location. Is there a way to that?
I have yet to create the change directory command. I was wondering if my problem would be solved through that instead. Any help is appreciated.
Side note: The instructions state that I "will need to provide some command line parsing capability to extract the target directory for listing." I have no idea what that is, but maybe someone could enlighten me.
Take a look at GNU's Simple Directory Lister example code, which uses opendir().
To parse command line arguments, take a look at your main() function:
int main(int argc, char* argv[])
{
...
return 0;
}
Use the argv[] pointer and argc integer value to determine the number of and character pointers of arguments.
In C programming how do you get the current user and the current working directory.
I'm trying to print something like this:
asmith#mycomputer:~/Desktop/testProgram:$
(user) (computerName) (current directory)
I have the following code, but the username is showing as NULL. Any ideas what I'm doing wrong?
void prompt()
{
printf("%s#shell:~%s$", getenv("LOGNAME"), getcwd(currentDirectory, 1024));
}
Aside from the fact that you should be using the environment variable USER instead of LOGNAME, you shouldn't be using environment variables for this in the first place. You can get the current user ID with getuid(2) and the current effective user ID with geteuid(2), and then use getpwuid(3) to get the user name from the user ID from the passwd file:
struct passwd *p = getpwuid(getuid()); // Check for NULL!
printf("User name: %s\n", p->pw_name);
To get the current computer name, use gethostname(2):
char hostname[HOST_NAME_MAX+1];
gethostname(hostname, sizeof(hostname)); // Check the return value!
printf("Host name: %s\n", hostname);
On unix-like systems use the function getlogin from unistd.h.
This is not a C question but is more like a UNIX question. There is no portable way of getting the username and current working directory in C language.
However, by viewing your example, I can tell you are trying to print the current UNIX user name and current working directory.
If you need current working directory in UNIX check getcwd function.
If you need current user name you can either call a separate whoami process within your C program, or check the getuid function call.
This is going to be platform specific, as there is no intrinsic way in the C programming language to do this.
It looks like you're on a Unix-based system, so you'll probably want to get the environment variable USER which is usually the logon name.
you can also use
#include<stdlib.h>
main()
{
system("echo %username%"); /* This is for Windows
* instead use system("echo $USER"); for UNIX
*/
}
Note that this will work on a unix system only.
may be LOGNAME was not set as your environment variable
you can see the environment variables using the printenv command
printf("%s#shell:%s$", getenv("USER"),getenv("PWD"))
Also does the job.
but as mentioned you shouldn't rely on environment variables, rather use the standard c functions.If you really want to use them, first make sure it is set.