C - fopen text file after building - OpenWRT - opkg - c

I have these lines in my C program:
int main(int argc, char **argv) {
int i=0, p=0;
FILE* fp;
fp = fopen("jacina.txt", "w+");
fscanf (fp, "%d", &i);
if (ftruncate(fp, 0) == -1) {
perror("Could not truncate")
};
p = i+10;
fprintf(fp, "%d", p);
}
After building this code to OPKG in OpenWRT (from Ubuntu), how can I read and write to this textual file which is located on any disk location where is located this OPKG?

Your code doesn't make any sense. To write the input given by user to a file:
Create a file first. Take input from user (say any string) and write it to the file with the help of file descriptor (fp) and close the file so that all buffers get flushed.
FILE *fp;
char comment[100] = {0};
fp=fopen("tempfile.txt","w");
if (fp == NULL)
{
printf("Error opening file!\n");
exit(1);
}
printf("Enter String: ");
gets(comment);
fwrite(comment, sizeof(comment), 1, fp) ;
fclose(fp);
fprintf() too can be used instead to write data into a file.
Similarly to read from a file you can use fgets() or fread() to store the contents of the file in a buffer and display the contents of the file. Hope it helps.

Related

Why are contents of a binary file not being printed in c?

Hello I am writing a program that reads the contents of a binary file and prints them to the screen.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr;
char filename[100];
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "rb");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
fseek(fptr,0L,SEEK_END);
int fsize = ftell(fptr);
fseek(fptr,0L,SEEK_SET);
unsigned char *c = malloc(fsize);
fread(c,fsize,1,fptr);
fclose(fptr);
printf("%s",c);
return 0;
}
but it does not print anything.Can someone explain me why and how should I fix this problem.
What you have attempted is not at all what you wanted to achieve.
Remember printf() formats the data it prints. To be printed properly with the %s formatting, the binary data values must be ASCII values but , of course, they are not.
You should probably attempt to printf() with %d.

Segmentation Fault -- Unable to write contents of one file to another

I can't copy the contents of one file to another in C because there is a segmentation fault occurring and I don't know the cause.
I know it has something to do with the syntax of fgets or the way I am giving the size of the buffer.
char* argument = argv[2];
char buffer[argc + 1];
FILE *fp;
FILE *quiz_log;
fp = fopen(argument, "r+");
quiz_log = fopen("quiz.log", "a");
fgets(buffer, 80, fp);
memcpy("quiz.log", buffer, 80);
fclose(quiz_log);
fclose(fp);
Expected: Successful write to file "quiz.log"
Actual: Segmentation Fault: 11
argc has nothing to do with the size of the file, it's the number of command line arguments. So there's no reason to use it as the size of the buffer.
Rather than try to read the file all at once, use a fixed-size buffer and read the file in a loop. Use fread() rather than fgets(), since that just reads one line.
You need to use fwrite() to write to the output file, not memcpy().
#define BUFFER_SIZE 1000
char* argument = argv[2];
char buffer[BUFFER_SIZE];
FILE *fp;
FILE *quiz_log;
fp = fopen(argument, "r");
if (fp == NULL) {
printf("Unable to open input file\n");
exit(1);
}
quiz_log = fopen("quiz.log", "a");
if (quiz_log == NULL) {
printf("Unable to open quiz.log\n");
exit(1);
}
size_t n;
while ((n = fread(buffer, 1, BUFFER_SIZE, fp)) > 0) {
fwrite(buffer, 1, n, quiz_log);
}
fclose(quiz_log);
fclose(fp);

unable to read a file in c language. it creates a new file instead of reading it

I have creates a file in directory and want to read it in. I am using the following code to open and read a file in C-Language. But it creates a new file instead of reading the old file.
int main()
{
FILE * file;
file = fopen ("file", "r+");
//file reading code
fclose(file);
return(0);
}
you are using 'r+' mode to open the file. It creates a new file if not already exist in the directory. see the following code for your help.
int main()
{
FILE * file;
file = fopen ("file", "r");
if(file !== NULL)
// to do file reading code
else
printf("error in reading file");
fclose(file);
return(0);
}
also check the file name that you are using in fopen() function.It is case sensitive and also check the extension of that file e.g; .txt or .data or what ever. e.g;
file = fopen ("File.txt", "r");
If you only intend to read from a file then open it for reading, i.e. mode r (notice no +):
file = fopen ("file", "r");
I agree with answers given but you can also additionally check if file is present or not. Something as below. In case program does not recognize the file it will let you know. Avoid using r+ in case you want to read an already existing file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}

Storage manager function to implement read operations on a file

How to adapt my program to perform the following operations:
1.To read first block of a file
2.To read current block of a file
3.To read previous block of a file
4.To read next block of a file
5.To read last block of a file
The following is the code written by me to read a data file using a file pointer and I want to implement the above operations
void main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened \n");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
}
Any suggestions would be helpful.

Storage manager function to read block of a file

I am implementing a storage manager to open, create, read and write a file using file pointers and file handler. But i am not able to write efficient code to implement all of my read functions of my storage manager. The read function of my storage manager has to implement the following:
To read first block of a file
To read current block of a file
To read previous block of a file
To read next block of a file
To read last block of a file
Any suggestions and code examples would be helpful to me.
The following is the code written by me to read a data file using file pointer -
*/
#include
#include
void main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened \n");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
ch = fgetc(fptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
}

Resources