Creating directories and files within - c

I've seen a lot of tutorials online about how you can edit files with c, how to write data within a text file and similar things.Like this code:
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
What I want to do is to make a folder, in VS there are four folders for use I.E
External Dependencies,
Header files,
Resource files,
Source files;
I'd like to make it possible to create another folder and within it store text files or another directories.
And after then I can delete the file or directory. Like that I can create the file within the code without any need to do that outside the programme.

Related

usage of tmpfile() in C

I don't understand why my script below seems to work without creating any files.
script.c:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
printf("P_tmpdir is '%s'\n", P_tmpdir);
FILE *tmp = tmpfile();
if(tmp == NULL){
printf("Unable to create temp file");
exit(1);
}
else{
printf("Temporary file is created\n");
}
for(int i = 0; string[i] != '\0'){
fputc(string[i], tmp);
}
rewind(tmp);
while(!feof(tmp)){
putchar(fgetc(tmp));
}
sleep(3);
return(0);
}
The P_tmpdir variable returns me the "/tmp" directory although in the sleeping time no new file is created in it... can you help me or explain me plz ?
Quoting cppreference.com (emphasis mine):
On some implementations (e.g. Linux), this function actually creates, opens, and immediately deletes the file from the file system: as long as an open file descriptor to a deleted file is held by a program, the file exists, but since it was deleted, its name does not appear in any directory, so that no other process can open it.
The file does not have to be "visible" in the file system tree, as long as a process has a handle on it, the file continues to exist.
If you want a file that's visible in the file system tree you should use mkstemp.

cant write text file in c

Im trying to learn c, I'm using tutorialspoint, and the function they give me doesn't do anything on my computer, the function is:
#include <stdio.h>
int main (){
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Am I missing something?
It is good to introduce some error checking with file streams
Do
fp = fopen("test.txt", "w+");
/*
* Try creating the file in the same folder for a start
*/
if(fp!=NULL)
{
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
}
else
{
/* There are multiple reasons you can't open a file like :
* You don't have permission to open it
* A parent directory doesn't exist and so on.
*/
printf("Can't open the file for write\n");
}
fclose(fp);
It creates a new file test.txt in /tmp directory and writes two lines using two different functions. Try find test.txt inside /tmp folder.
fopen() won't create directories for you.
You need to create a tmp folder at the root of your current disk before running this program.
First you need to create the temp directory from where you executing this code because fopen is not creating the directory so,than after you need to check following code:
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
if(fp == NULL)
{
printf("Usage Message: File is not open temp/test.txt");
}
else
{
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
}
Also remember that when you dealing with file operation always you have to check your file is opened/create or not using Usage message. Actually it is good sign of programming.
I know this is a bit late, but I found the code work with the following on my PC...
instead of "/tmp/test.txt", make it "tmp/test.txt".
Yes, just remove the "/".

Not able to read data in from a text file.

Im having a problem finding out where to store my Text File which I want to read from. I stored it in the same directory as the executable file but it is still not reading the file in. After much digging and searching I have found that the problem has something to do with the "Derived Data" in Xcode. When I look for the source file , by right clicking on the main.c file and locating the file in finder I find that it is located within the Derived Data. Whenever I run this program it creates some derived data (which Xcode is supposed to do) but my .c file is stored within this derived data and when I try and store the text file in the same location the executable is located (the same place the .c file is located) I am not able to read in the file.
Suggestions?
Ideas?
Help..
#include <stdio.h>
#include <unistd.h>
extern int errno;
int main (void){
int term;
long long StudentID;
char lastname [16];
char firstname[16];
char subject[4];
int catalog;
char section[4];
char filename [9];
FILE *cfPtr;
printf("Enter file name: ");
scanf("%s",filename);
/*
char cwd [1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "Current working dir: %s\n", cwd);
*/
system("pwd");
if((cfPtr=fopen(filename,"r"))==0){
//printf("%s jlk",strerror(errno));
printf("File could not be opened\n");
}
else{
printf("%-35s%-10s%-10s%-10s%-10s\n","Last Name, First Name","Term","ID","Course","Section");
int i;
for(i=0; i<75; i++){
printf("-");
}
fscanf(cfPtr,"%d%lld%s%s%s%d%s",&term,&StudentID,lastname,firstname,subject,&catalog,section);
while(!feof(cfPtr)){
printf("%-35s%-10s%d%lld%-10s%-10s\n", lastname, firstname,term,StudentID,subject,section);
fscanf(cfPtr,"%d%lld%s%s%s%d%s",&term,&StudentID,lastname,firstname,subject,&catalog,section);
}
fclose(cfPtr);
}
return 0;
}
If you've set up a "Command Line Tool" project in Xcode, then you can set the working directory as follows. First click on the project name at the top of the Xcode window and select "Edit Scheme..."
Then select "Run Debug" on the left and change the "Working Directory" on the right.
The alternative is to download the command line tools (Preferences/Downloads). Then you can open a terminal window, and compile/run from the command line.

can not create text files in a c program

I want to create 1000000 text files in a folder, by a c program, but it creates only 508 text files in a folder. My code is:
int i, j;
char temp[100];
for(j=0;j<100;j++)
temp[j]= NULL;
for (i=0;i<1000000;i++)
{
sprintf(temp,"%ld.sift",i);
fopen(temp,"w+");
}
Also call fclose after you've created the file - you can't have 1000000000000 files opened at the same time.
Use the below code:
int i, j;
char temp[100];
FILE *fp;
memset(temp , '\0' , 100 );
for (i=0;i<1000000;i++)
{
sprintf(temp,"%ld.sift",i);
fp = fopen(temp,"w+");
fclose(fp);
}
As others pointed out by others the code seems to be missing to fclose() the file fopen()ed, so the system most probably runs out of resources keeping all those file open.
To fix this just close them, before creating new ones.
If on Linux and you just want to create the (empty) files without opening them the mknod() function can be used for this.

how to access particular file in folder through file handling in c

I have suppose two text file abc.txt and def.txt in folder "my". I have a programme which directly goes to that folder and search particular file and if that particular file find out then how to access that file's information.
I know how to read write file in C through file handling but I have no idea how to search particular file and after that read that particular file to match particular string in file.
**All these things access through file handling in C.**
So please if any one have any solution I will be thankful for that
Example will be best way to understand .
Thanks in advance
To get a listing of the files in a directory in Linux, you can use the 'opendir', 'readdir' and 'closedir' functions from 'dirent.h'. For example:
#include <dirent.h>
#include <stdio.h>
int ListDir(const char *pDirName)
{
DIR *pDir;
struct dirent *pEntry;
pDir = opendir(pDirName);
if (!pDir)
{
perror("opendir");
return -1;
}
while ((pEntry = readdir(pDir)) != NULL)
{
printf("%s\n", pEntry->d_name);
}
closedir(pDir);
return 0;
}

Resources