Unexpected output for reverse file reading in C language - c

Here is my code but I don't know why it prints only some part of lines.
here is my code:
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
FILE *fpr,*fpw;
int cnt=0;
fpw=fopen(argv[2],"w+");
char buff[1000];
while((fpr=fopen(argv[1],"r"))==NULL)
{
printf("\nCan't open file %s\n",argv[1]);
scanf("re-enter file name:%s\n",argv[1]);
}
while (!feof(fpr))
{
fgets(buff,2,fpr);
if(buff[0]=='\n')
{
putc(buff[0],fpw);
fseek(fpw,0,SEEK_SET);
}
fputs(buff,fpw);
cnt++;
}
fclose(fpr);
fclose(fpw);
}
INPUT FILE:
Hrey yhis will print twicw
lets print thrice
#why not
OUTPUT FILE
lets print thrice
#why not

So.. basically what I was trying to do is actually impossible using lseek/fseek. So I found another approach , as shown in the code below:
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE* fr;
FILE* fw;
int l=0;
char lines[10000][100];
if( ( (fr=fopen(argv[1],"r+"))==NULL || (fw=fopen(argv[2],"w+"))==NULL ) )
{
printf("Error reading or opening files %s,%s",argv[1],argv[2]);
}
while(fgets(lines[l++], sizeof(lines[l]), fr)!=NULL);
while(l>=0)
fputs(lines[l--],fw);
fclose(fr);
fclose(fw);
}

Related

executable file in C

I tried to write a program in C that checks if a certain file is executable or not, if it is a shell script or a binary
<apue.h> is a header from the book Advanced Programming in the UNIX Environment
I don't think the approach is exactly right (if it's a shell script or binary). I think there is a more efficient solution. Which one do you think?
In addition, what other problems does the code have?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <apue.h>
void checkIfFileExists(const char *fileName);
int main (int argc,char *argv[])
{
char *fileName = argv[1];
if (argc < 2 )
{
err_quit("File to check not specified\n");
return 0;
}
if (argc > 2 )
{
err_quit("Too many arguments\n");
return 0;
}
checkIfFileExists(fileName);
return 0;
}
void checkIfFileExists(const char *fileName)
{
if(!access( fileName, F_OK ))
{
if(!access( fileName, X_OK ))
{
printf("The file %s is an executable\n",fileName);
//check if the file is binary or shell script
}
else
{
printf("The file %s is not an executable\n",fileName);
}
}
else
{
err_quit("The file %s was not found\n",fileName);
}
}
You can use the stat methods,
#include <sys/stat.h>
...
struct stat myf;
stat(<file>,&mf);
if( mf.st_mode & S_IXUSR )
printf("execute");
The output is a bitmap from which you can extract the file permission info
look at https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html
for the permission define available.

I'm having trouble reading a file in C, where the first line is is different

I'm working on a task where I need to read a long .txt file. The first line contains the number of lines the .txt file has, the rest of the lines follow the same structure, "int int int char:char".
How do I read the first line separately from the rest?
I wrote the following code:
FILE *fajl;
falj = ("musor.txt", "r");
while (!feof(fajl) && fajl > 1)
{
fscanf_s(fajl, "%d %d %d %[^:]c:%c\n", &tomb[i].ado, &tomb[i].perc, &tomb[i].masodperc, &tomb[i].eloado, &tomb[i].cim);
i++;
}
Sorry for the unknown words, the variable names are in Hungarian.
So this is basically just the collection of all the comments from the question.
//-------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Tomb
{
int masodperc, eloado, ado, perc;
char cim;
};
typedef struct Tomb Tomb;
//-------------------------
int main(int argc, char *argv[])
{
char linesLength[10], stringFajl[100];
FILE *fajl;
fajl = fopen("musor.txt", "r");
if(fgets(linesLength, 100, fajl)==NULL || fajl == NULL) //first line read
{
printf("error\n");
return -1;
}
int length = atoi(linesLength), i=1;
Tomb tomb[length];
while (fgets(stringFajl, 100 ,fajl )!=NULL || i<=length)
{
sscanf(stringFajl, "%d %d %d %d [^:]c:%c", &tomb[i].ado, &tomb[i].perc, &tomb[i].masodperc, &tomb[i].eloado, tomb[i].cim);
i++;
}
fclose(fajl);
return 0;
}

C-Segmentation fault (core dumped) error in linux

#include <stdio.h>
#include <stdlib.h>
struct fileIndex{
char name;
int key;
} index1;
int main(int argc, char *argv[]){
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
for(i=0; i<10; i++){
printf("%c",fgetc(pFile));
}
fclose(pFile);
}
When I want to run my program, it gives that error. I looked so long for a wrong line in code, but I didn't find any. Can you help me ?
If the file failed to open, that will make pFile equal NULL, which can easily cause fgetc() to segfault.
You must check for this before trying to read from the file:
if (pfile == NULL)
{
perror("Failed to open file");
exit(1);
}
change your code as
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
if(!pFile)
return;
Also.. looks like you file path is misplaced... are your meant t ouse /cat/home/sysadmin/deneme.txt
Is cat your current directory or part of absolute path

What is wrong with this code of file handling in c?

I am trying to write content in a file from terminal. File is creating but content is not written into the file.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc, char *argv[])
{
FILE *fp;
fp=fopen(argv[1],"w");
char ch;
while((ch=getchar())!=EOF)
{
putc(ch,fp);
}
fclose(fp);
return 0;
}
If you don't signal EOF (Ctrl+Z in Windows and Ctrl+D in Linux), then the loop will continue to execute until it receives that signal.
If you attempt to read the file with your own eyes while the program is still on execution, then the file stream will not have close (fclose(fp); will not have execute), thus the file will appear to you empty, even though the content will be shown to you, when the file stream closes.
The following works fine:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc, char *argv[])
{
FILE *fp;
fp=fopen(argv[1],"w");
char ch;
while(1)
{
ch = (char)getchar();
putc(ch,fp);
if(ch == '.') break;
}
fclose(fp);
return 0;
}

put a file in argv[] of main function

hi I'm trying to write a program like this in visual C++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int argc, char *argv[])
{
FILE *in;
char ch;
int openbracket=0,closebracket=0;
if(argc!=2)
{
printf("the number of arguments is incorrect");
getch();
exit(1);
}
if((in=fopen(arg[1],"r"))==NULL)
{
fputs("Error",stderr); exit(1);
}
ch=getc(in);
while(!feof(in))
{
if(ch=='{')
openbracket++;
else if(ch=='}')
closebracket++;
ch=getc(in);
}
printf("Open bracket==%d,close bracket=%d",openbracket,closebracket);
getch();
}
i am trying to open a file with argv[1] in this program in visual c++
can you please show me how i can put a file in argv[1] 0f main function?
Thank you
if((in=fopen(arg[1],"r"))==NULL);
if-statements do NOT end with a semi-colon. That makes this a "do-nothing" statement
arg is never declared anywhere (you have declared argc and argv, but not arg)

Resources