Iam trying to print the content of a text file called text.txt in the terminal in c I have the following code but it doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
char str[255];
FILE *fp;
fp = fopen("text.txt","r");
while (fgets(str,255,fp)!=NULL){
printf("%s",str);
fclose(fp);
};
}
I couldn't find a solution please help
First of all, you are closing the file inside the loop. fclose should lie at the end of your program. Secondly, fopen() might fail (you don't have permission to read the file for example). So, don't forget to handle that too.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
const int sz = 255;
char str[sz];
FILE *fp;
fp = fopen("input.txt","r");
if(fp == NULL){
// opening the file failed ... handle it
}
while (fgets(str,sz,fp)!=NULL){
printf("%s",str);
};
fclose(fp);
}
Here is another similar way
if (fp!=NULL)
{
// file open succeded. do sth with it.
fclose (fp);
}
Hope this helps and keep coding!
Related
I am trying to open the file indicated by the input file[100]. I keep getting a segmentation fault. I know that there is some problem with the fgets() function, but I am not exactly sure what it is. I can't get any code inside the loop to run. The final code is supposed to take the file from the first line of a text file, but it does not work. Any help would be appreciated, and I can post the main function code if needed!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numInt.h>
#include <string.h>
char *getCoeff(char file[100], char chemName[50])
{
FILE* fptr = fopen(file, "r");
char data[80];
char *dataptr = data;
char line[80];
int count = 0;
int i = 0;
printf("%s",file);
while (fgets(line, sizeof line, fptr) != NULL)
{
printf("Inside loop\n");
printf("%s",line);
strcpy(data,line);
}
fclose(fptr);
return dataptr;
}
I used C program to write into an excel file and it worked for both (.xls & .xlsx)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen ("cfile.xls","w");
fprintf(fpointer, "Name \t MIT 801\t MIT 802\t MIT 803\t MIT 805\t MIT 821 \n Haphyz\t 90\t 89\t 99\t 95\t 96\n");
fclose(fpointer);
}
I also tried to read from it and it also worked:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen ("cfile.txt","r");
char Creader[200];
while (!feof(fpointer)){
fgets (Creader,200,fpointer);
puts (Creader);
}
fclose(fpointer);
return 0;
}
Now I'm trying to use C program to read an excel file that was created from the computer (not from C) but all I get is gibberish...nothing meaningful for both .xls and .xlsx then I decided to use the .csv format which worked but was only able to read only the first line of the excel sheet. Please how do I go about this?
I have resolved the issue, the .csv format works fine the problem was with my code. I closed the file inside the loop so the program stops reading after the first line .
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen("Book1.csv","r");
char spreadSheet[200];
while(!feof(fpointer)){
fgets(spreadSheet,200,fpointer);
fprintf(stderr, "%s\n", spreadSheet);
fclose(fpointer);
}
return 0;
}
There is no way this would return an error message ,so it printed out what I instructed it to .
Thanks for your contributions.I'll make sure to look at my code better next time
I'm currently working on an old coding problem from USACO in C. Here are the first couple lines of my code, in which I am trying to use the fscanf() function to grab the first value, an int, from the blocks.in file:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fin = fopen ("blocks.in", "r");
FILE *fout = fopen ("blocks.out", "w");
int i,j;
int linecount = 0;
int alphabetCount[26];
fscanf(fin," %d",&linecount);
Running gdb (as a part of the Eclipse C/C++ IDE), I consistently get a segmentation fault error on the line:
fscanf(fin," %d",&linecount);
The error consistently reads:
No source available for "flockfile() at 0x7fff855e6d39"
I haven't been able to source the issue. I've not had any problems with this in the past. Do you see what is wrong, or have a better solution/function with which to extract the data?
I suspect that there is no blocks.in file in the directory from which you run the program. Even if the file is present, it may not open successfully. Some simple error-checking could help you avoid problems here:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fin;
FILE *fout;
int i,j;
int linecount = 0;
int alphabetCount[26];
if ((fin = fopen("blocks.in", "r")) == NULL) {
fprintf(stderr, "Unable to open input file\n");
exit(EXIT_FAILURE);
}
if ((fout = fopen("blocks.out", "w")) == NULL) {
fprintf(stderr, "Unable to open output file\n");
exit(EXIT_FAILURE);
}
fscanf(fin," %d",&linecount);
return 0;
}
I am trying to read a file character by character and print it on screen.
However, the character is not displaying, I am getting a box with 0001 in it.
This is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "rb");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 1;
}
you need to check the return values from fopen, to ensure you opened the file successfully, you could be executing from the wrong directory.
Plus if your file is a TEXT file, you should be opening using "rt".
Basic File Opening modes in C is
"r"-reading
"w"-writing
"a"-append
"r+"-reading+writing
"w+"-reading+writing
"a+"-"reading+appending"
This code is enough to read .txt files
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "r");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 0;
}
log.txt :
Hello world
world is not enough
to show our knowledge
cpp file :
#include <stdio.h>
#include <string.h>
int main()
{
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
fscanf(fp, "%512[^\n]", szLine);
puts(szLine);
getchar();
}
return 0;
}
Acually expected output for this program has to read line by line. But it read only first line alone. What is the mistake on this code. thanks advance.
you can change your program like this:
#include <stdio.h>
#include <string.h>
int main()
{
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
fgets(szLine,512,fp);
puts(szLine);
getchar();
}
return 0;
}
because the offset of the file stream always be 0, so you read
"%512[^\n]" does not read the endline character. The remaining part of the file starts with \n and fscanf fails to read the line(it return 0 instead of 1). You need to read the endline character !
#include <stdio.h>
#include <string.h>
int main()
{
int bla;
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
bla= fscanf(fp, "%512[^\n]", szLine);
if(bla==1){
printf("%d\n",bla);
puts(szLine);
getchar();
fgetc(fp);
}
}
return 0;
}
Using getline() may be a more reliable solution.
getline(&szLine,&size,fp);
Bye,
Francis
try this
while(fscanf(fp, " %512[^\n]", szLine)==1)
{
puts(szLine);
getchar();
}