Copying contents of text file using file handling - c

I created a text file named "test.txt" and then executed this program to copy the contents of "test.txt" to "file.txt". But it is showing an error while opening the file itself, i.e., fr==NULL is true.
What is wrong with the program?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i,count=0;
char ch;
FILE *fw,*fr;
fw = fopen("file.txt", "a+");
fr = fopen("test.txt", "r+");
fseek(fr,0,SEEK_SET);
if(fr==NULL)
{
printf("Error while opening the file.\n");
exit(0);
}
while((ch=getc(fr))!=EOF)
{
putc(ch,fw);
}
fclose(fw);
fclose(fr);
return 0;
}

If fopen returns NULL, check the value of errno, e.g. by including <errno.h> and using the perror("Error while opening the file"); instead of the printf statement.

Related

unable to read text from file in C

I have this code that's supposed to read from a text file and print its content but I keep getting "Unable to open file."
#include <stdio.h>
#include <string.h>
#define MAX 4000
int main(void)
{
FILE *bkptr;
char buffer[MAX];
bkptr = fopen("defoe-robinson-103.txt", "r");
// If file dow not open
if (bkptr == NULL) {
puts("Unable to open file.");
}
// If file open
else {
// Read each line form file until EOF
while (fgets(buffer, MAX, bkptr)) {
// Print the line
puts(buffer);
}
// Close the file
fclose(bkptr);
}
}

I want copy my present working directory location into a new text file

I want to copy my present working directory location into new text file. For example, I am working in "xyz" directory and I want copy xyz directory path into abc.text file.
Is it possible in Centos or any Linux operating system?
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
FILE *fp;
char buf[PATH_MAX];
if (getcwd(buf, sizeof(buf)) == NULL) {
perror("getcwd");
exit(1);
}
fp = fopen("abc.txt", "w");
if (fp == NULL) {
perror("fopen");
exit(1);
}
fprintf(fp, "%s\n", buf);
fclose(fp);
return 0;
}
Shell
pwd > abc.txt

Debugging C program with two arguments passed

I have fixed some syntactical errors in my code and now the program compiles fine. But when I execute the program the outputFile is empty. outputFile should have contents of inputFile in reverse order. I am trying to debug code in CodeLite IDE.
I need to debug the code with two arguments passed (inputFile and outputFile). I don't seem to find that option in CodeLite IDE. How do I do that ?
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFER_SIZE 256
int main (int argc, char *argv[]){
FILE *inputFile, *outputFile;
int fileSize;
int pointer;
char buffer[BUFFER_SIZE];
/* Check for correct user's inputs. */
if( argc !=3 ) {
fprintf(stderr, "USAGE: %s inputFile outputFile.\n", argv[0]);
exit(-1);
}
/* Make sure input file exists. */
if( (inputFile = fopen(argv[1], O_RDONLY))) {
fprintf(stderr, "Input file doesn't exist.\n");
exit(-1);
}
/* Create output file, if it doesn't exist. Empty the file, if it exists. */
if((outputFile = fopen(argv[2], "a+"))) {
fclose(inputFile);
exit(-1);
}
/* Find the size of the input file. */
fileSize = fseek(inputFile, 0, SEEK_END);
/* Read input file and write to output file in reversed order.*/
for(pointer=fileSize-1; pointer>=0; pointer--) {
/*Write content in the buffer to the output file */
while(!feof(inputFile))
{
fgets(buffer, BUFFER_SIZE, inputFile); //reads 256 bytes at a time
fputs (buffer , outputFile );
}
}
fclose(inputFile);
fclose(outputFile);
return(0);
}
http://codelite.org/LiteEditor/ProjectSettings:
Project Settings >> General >> Command arguments
Right click on the project folder
Select project settings
General -> Execution -> Program Arguments

How to create a folder in c?

I tried to create a file and I did.
Now I'm trying to create the file inside a new folder, but this code doesn't work!
#include <stdio.h>
#include <stdlib.h>
int main( void)
{
FILE *fp;
fp = fopen("txt/example.txt", "w"); // This only works without "txt/"
fprintf(fp, "%s", "Some data here");
fclose(fp);
return 0;
}
Maybe I need to create the folder before and only after the file, but I don't know how to achieve it... any help is appreciated!
This example makes the directory before it creates the file, and when it makes the file, note the double \\ in the file name to prevent an escape sequence being attempted from \e, although it does work with a single / too.
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
void fatal(char *msg) {
printf("%s\n", msg);
exit (1);
}
int main(void) {
FILE *fp;
if (mkdir("txt"))
fatal ("Error creating directory");
if ((fp = fopen("txt\\example.txt", "w")) == NULL)
fatal ("Error opening file");
if (fprintf(fp, "%s", "Some data here") <= 0)
fatal ("Error writing to file");
if (fclose(fp))
fatal ("Error closing the file");
return 0;
}
You need to use CreateDirectory to create a directory on windows.

unable to open file which is in d: drive

How to read and write a file which is not in the bin directory that is it is out of the C drive.
I wrote this code
fs=fopen("d:/source.txt","w");
if(fs==NULL)
{
puts("Unable to open file");
}
And it is outputting "Unable to open file". Can someone please help me out.
FILE *fs= fopen("d:/source.txt","w");
if(fs==NULL)
{
printf("can't open");
}
if (fs!=NULL)
{
fputs ("Opened successfully",fs);
fclose (fs);
}
Make sure the source.txt file exists and it is not read only. I tried above code didn't get any error.
There are several possible reasons why the file cannot be opened via fopen().
To get information on details of the error print out errno and/or call perror() and/or strerror() like for example so:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
char filename[] = "d:/source.txt";
FILE * fs = fopen(filename,"w");
if (NULL == fs)
{
perror("fopen() failed");
fprintf(stderr, "Error #%d occurred when trying to open file '%s': %s.\n",
errno,
filename,
strerror(errno));
}
...
return 0;
}

Resources