Write each file name and content to another file in C - c

So I'm sort of new to C language, and I'm trying to create a .dat file from other files (like some archiving program). I'm using arguements to specify the files in command line I want to write to my .dat file, and the pack function I wrote seems to be working.
The problem is, I want to create an unpack function too. I'm not sure if this is the easiest way to do this, but I want to copy the argument (the name of the file I want to pack) to it's content, so later, in my unpack function I could search for it inside the .dat file and unpack it.
I'm using this code to write the files to the .dat:
if(!wanttoArchive){
printf("Cannot open %s file\n",argv[i]);
} else {
fseek(wanttoArchive, 0L, SEEK_END);
pos = ftell(wanttoArchive);
fseek(wanttoArchive, 0L, SEEK_SET);
while (pos--){
ch = fgetc(wanttoArchive);
fputc(ch, archived);
}
}
I wanted to use strcat inside the fputc, put it's not working.
Any kind of help would be greatly appreciated!

Related

How do you get rid of file contents without the filename?

I have a function that takes in a file pointer (FILE * file) and copies everything in that file. Now, is it possible to erase everything inside this file?
I do not have the file name so I can not use fopen(filename, "w") again.
I have stdio.h and string.h included.
You can use the ftruncate() system call to empty a file using its file descriptor, e.g.
ftruncate(fileno(fh), 0);
You will probably want to follow that up with a call to rewind(fh) so that any further writes to the file are made at the beginning, rather than at the previous offset.

Append Random Text to File (C)

I'm trying to create a file inside a directory, then append some random text inside of the file.
My Code
char dirname[30];
sprintf(dirname, "myroom.%d", (int)getpid());
mkdir(dirname,0777);
char path[path_max+1];
snprintf(path1, PATH_MAX+1, "%s/file1.txt,dirname);
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
char *random_name = { "burger", "toast", "burrito", "noodles" };
int number = rand();
fputs(random_name[number], filedir1];
What I want
(Inside directory "dirname")
When I open file1.txt, I expect there will be either the word burrito, burger, toast, or noodles in the first line.
What I get
file1.txt still empty.
Questions
Anybody know what happen with my code? I saw from youtube video, to append some text into a file, all I need is the fputs command but it doesn't seem to work in my code. Is it because I'm using "a+" in fopen?
Any help will be highly appreciated. Thanks
I think that rand() is creating problem.use rand()%4 instead.
And you are closing the file using fclose() before putting random word. Use it at last.
After opening a file, you are close that file. After closing the file, you are not able to enter the text to that file.
FILE *filedir1 = fopen(path1, "a+");
fclose(filedir1);
Your problem is in this fclose line. You have to do this in after finishing the write operation in that file.
So, remove the fclose(filedir1) and put this line after writing the file.

Creating a file using fopen()

I am just creating a basic file handling program.
the code is this:
#include <stdio.h>
int main()
{
FILE *p;
p=fopen("D:\\TENLINES.TXT","r");
if(p==0)
{
printf("Error",);
}
fclose(p);
}
This is giving Error, I cannot create files tried reinstalling the compiler and using different locations and names for files but no success.
I am using Windows 7 and compiler is Dev C++ version 5
Change the mode argument in fopen(const char *filename, const char *mode) from:
p=fopen("D:\\TENLINES.TXT","r");//this will not _create_ a file
if(p==0) // ^
To this:
p=fopen("D:\\TENLINES.TXT","w");//this will create a file for writing.
if(p==NULL) // ^ //If the file already exists, it will write over
//existing data.
If you want to add content to an existing file, you can use "a+" for the open mode.
See fopen() (for more open modes, and additional information about the fopen family of functions)
According to tutorial, fopen returns NULL when error occurs. Therefore, you should check if p equals NULL.
Also, in printf("Error",);, omit the comma after string.
Yes you should open the file in write mode.
Which creates the file . Read mode is only to read content
or else you can use "r+" for both read and write.
You should be able to open the file, but you need to make it first. Make a txt document with the name res.txt. It should be able to write your result into the text document.
<?php
$result = $variable1 . $variable2 "=" .$res ."";
echo $result;
$myfile = fopen("res.txt", "a+") or die("nope");
fwrite($myfile, $result);
fclose($myfile)
?>
fopen()
Syntax:
FILE *fp;
fp=fopen(“data.txt”,”r”);
if(fp!=NULL){
//file operations
}
It is necessary to write FILE in the uppercase. The function fopen() will open a file “data.txt”
in read mode.
The fopen() performs the following important task.
It searches the disk for opening the file.
In case the file exists, it loads the file from the disk into memory. If the file is found with huge contents then it loads the file part by part.
If the file does not exist this function returns a NULL. NULL is a macro defined character in the header file “stdio.h”. This indicates that it is unable to open file. There may be following reasons for failure of fopen() functions.
a.When the file is in protected or hidden mode.
b.The file may be used by another program.
It locates a character pointer, which points the first cha
racter of the file. Whenever a file is
opened the character pointer points to the first character of the file

fopen using append on EXISTING files (malfunction)

Hi all I am relatively new to programming. Currently my code is working "correctly"
What I want to do is open a file for appending and then use couple different other source files to put data on the target file for appending.
The problem is that whenever I use fopen on the file name, it does not open the existing file but rather creates a file with no extension (all the files are in the same directory so should be able to open no problem). This is the code I am using to grab the necessary information for which file to append (error is never generated because it just creates a new file)
FILE *fp_target;
char target[100];
fgets(target, 99, stdin);
if ((fp_target = fopen(target, "a"))==NULL)
{
fprintf(stderr, "the following target file %s can not be appended", target);
exit (0);
}
Actually I tried this test a couple times and when i do this again with the same name used in the first try (lets say I used test.c) the program will open the test.c(no extension rather than the file "test extension (.c)")
can anyone please let me know what I am doing incorrectly?
also, when I fopen a file for reading it will read the file with the extension
Try to remove the newline from the input. By using
target[strlen(target)-1]='\0';
strlen() is used to find the length of the string which is available in string.h header file.
strlen(target)-1 which is used to find the newline location \0 the null character is pasted so the new line is removed.
If you worked with that new line the file is created as test.c?.
In disk we cannot create a file with the already exist name. So the newline is act as one character and the file name is created as like a existing name.
The problem is with your fgets(target, 99, stdin); line. After giving input 'test.c' it takes some garbage values till the end of the array. so every time it creates a new file. Instaed of fgets use scanf to get the input from user.
scanf("%s",target);
if ((fp_target = fopen(target, "a"))==NULL)
{
fprintf(stderr, "the following target file %s can not be appended", target);
exit (0);
}
If you want to use fgets to read the input from the user, after the user input make it NULL.
fgets(target, 99, stdin);
target[strlen(target)-1]='\0';

query regarding writing in a file using c

Assume that the files are .txt
Contents of first file
hello how are you
Contents of second file
I am fine
The desired result is
hello how are you I am fine
Normally what happens is that the original contents are removed and then new contents are added in it.
I want to write in the first file in such a way that its original contents are maintained and the contents of second file are concatenated in it.
Is there any way to do that?
you can append another string in file by opening it in appending mode.
FILE *fp;
fp=fopen("file.txt","a");
here the next string will append after the last pointer of file.For more info link.
Yes, you can open the file with:
fopen("fileName", "a");
This will let you append to the end if the file.
More info is here: http://www.cplusplus.com/reference/cstdio/fopen/
It would help to know how you are trying to write the file. Likely, you are looking for the append option to FOPEN:
http://www.cplusplus.com/reference/cstdio/fopen/
FILE *f = fopen("foo.txt","a");
if (f != NULL) {
/* Use f */
fclose(f);
}

Resources