How to clear the contents of an open file in *nix - file

I would like to know how the contents of a file can be cleared in *nix if it is open to write. (It may be a log file for example.)

Take a look at fopen's manpage:
w
Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
so if you use
fp=fopen("file.txt", "w");
the contents of file.txt will be erased.
Update:
To delete a file's contents from command line use
printf "\0" > file.txt

Related

Reading a string from a file with C. Fopen with w+ mode is not working

I made a C program that reads a string from a .txt file, then it encrypts the string, and finally it writes the string in the same file.
The thing is that if I use fopen("D:\\Prueba.txt","w+"), the program doesn't work, it prints garbage like this )PHI N.
I've debugged and I know the error is there in that line, because if I use fopen("D:\\Prueba.txt","r+"), the program works, and it writes what it should.
But I want to use w+ because it will rewrite what the .txt file had. Why is w+ not working?
If you're opening with w+ to first read the content, that's not going to work. From C11:
w+: truncate to zero length or create text file for update.
What's probably happening is that you read data from the now empty file but don't correctly check that it worked. That would explain the weird "content" you see of )PHI N.
One solution is to open the file as with r, open another file with w, and transfer the contents, encrypting them as part of that process. Then close both, delete the original, and rename the new one to the original name. This will allow you to process arbitrarily-sized files since you process them a bit at a time.
If you don't want to use a temporary file, and you're sure you can store the entire content in memory, you could open it r+, get the content, the reopen it with a new mode, such as with:
FILE *readFh = fopen( "myfile.txt", "r+");
// Read in content, massage as needed.
FILE *writeFh = frepoen( NULL, "w+", readFh);
// Provided that worked, you should now have an empty file to write to.
// Write back your massaged data.

does append "a+" automatically put cursor to the end

I want to open a file with c then add some content to it and close it. I just want to know a+ in fopen automatically navigates to the last character of file.
Yes it does.
Why don't you try, or read the manual ?
Here it is :
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the
file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is
positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned
at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial
file position for reading is at the beginning of the file, but output is always appended to the end of the file.

File Modes in Binary

Well I was looking into ways to open a file in binary and I saw that you can open one for r+b and a+b but whats the difference because this is what it says for definition.
a+b :Open a file for both reading and writing in binary mode.
r+b: Open a file for both reading and writing in binary mode.
a+b
Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
r+b Open for reading and writing. The stream is positioned at the beginning of the file.
w+b Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
So, a+b will create the file if it does not exist, and it will append to the file when you write to it,
r+b will not create the file if it doesn't already exist (fopen() would fail), and writing to the file will start at the beginning of the file.

is there any option to create an output text file in the name of the input file in c programm?

is there any option to generate an output text file as same as the input file name?
in C i got the file name using:
gets(file_name);
and open it through the:
f1=fopen(file_name,"r");
comment.how do i open the file without entering the format type?
for example for file100.txt i'd like to enter file100 to read the file.
and any option to get the output file as same name as the input file?
You can use
snprintf(new_filename, sizeof new_filename, "%s.%d", File_name, int_val);
For your problem with the file name, you can use e.g. sprintf:
char full_file_name[256];
sprintf(full_file_name, "%s.txt", file_name);
This is not recommended without some validation of the entered file name of course.
For your other problem, from the documentation of fopen:
r+ Open for reading and writing. The stream is positioned at the beginning
of the file.
w+ Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated.
The stream is positioned at the beginning of the file.
a+ Open for reading and appending (writing at end of file). The file is
created if it does not exist.
The initial file position for reading is at the beginning of the file,
but output is always appended to the end of the file.
for creating output file with the same name, simply save your output content into some string. Then, close the file and open it again with write mode("w"). And then, write the content and close the file again.

C: read in more than one file

Hey guys using POSIX API system calls read, write, open, etc. I can open, read, write to a file and copy its contents to an output file. How would I go about copying more than one file to an output file using related system calls only?
I currently have:
filein = open(argv[1],O_RDONLY,0);
to open one file.(which is argv1 but I'd like to know how to do argv2 and argv3 etc.)
I tried :
j=0;
filein = open(argv[j],O_RDONLY,0);
but that prints out contents of argv0 into my outputfile.
I am stuck on the next stage to do more than one file. (I also have an EOF loop so after 1 file it exits-How would I make this continue for the next file).
Please could you help me with how to approach the next stage? Thanks.
Background
argv[0] is the name of the program.
argv[1] is the 1st command line parameter.
argv[2] is the 2nd command line parameter.
etc.
So:
Start your loop at 1, instead of 0 (i.e., j=0 is incorrect).
Be sure to close the file immediately after reading it and before opening the next file.
Algorithm
Think about the algorithm before writing the code.
Set counter to the index of the first argument.
Open the file.
Assign a handle to the open file.
Read the file contents.
Write (if required) the file contents.
Close the file using the handle.
Increment the counter.
Loop until there are no more command line arguments.
Now you can write the code.
You might get bonus points if you include error handling. (What happens when the file is missing, is not readable, the file system is corrupt, or the machine has run out of memory or disk space?)
Concatenating Files
If you want to concatenate two file names to a third, you need to rethink the algorithm, and what you need. There is a difference between "read the first two files given on the command line and write them to the third file" and "append all the files given on the command line to the last file given."
Read Two, Write One
The algorithm:
Make sure that there are exactly three parameters.
Create a file handle variable for the third file (output).
Create a file handle variable for the first file (input).
Create a file handle variable for the second file (input).
Open the first file for reading.
Open the second file for reading.
Open the third file for writing.
Read the contents of the first file and write them to the third file.
Read the contents of the second file and write them to the third file.
Close the third file.
Close the second file.
Close the first file.
You will notice a lot of redundancy at this point.
Read N, Write One
This algorithm is a bit more challenging, but removes the redundancy.
Make sure there are at least two parameters.
Open the last file for writing.
Loop over every file name up to, but not including, the last file name given:
Open the input file for reading.
Write the contents of the file to the last file.
Close the input file.
Close the output file.
For this you will need to understand argc and its relationship with argv. In pseudo-code:
if number_of_arguments < 2 then
print "This program concatenates files; two or more file names are required."
exit
end
int outfile = open arguments[ number_of_arguments ] for writing
int j = 1
while j < number_of_arguments do
int infile = open arguments[ j ] for reading
string contents = read infile
write contents to outfile
close infile
increment j
end
close outfile
Tutorials
If you are having trouble with C syntax, search for tutorials. For example:
http://www.cprogramming.com/tutorial/c/lesson3.html
Use a loop to read all the files. Start at 1 to skip the current executing process which is located at argv[0].
for(int i = 1; i < argc; ++i)
{
int filein = open(argv[i],O_RDONLY,0);
// ... process file
close(filein)
}
argv[0] is the name of the program. argv[1] is the first then you pass on the command line.
Open your output file then each input file. read each input file into the output file then close them all and exit.
to open one file.(which is argv1 but I'd like to know how to do argv2 and argv3 etc.)
fopen(argv[2], ...)

Resources