I'm trying to take input from the user and compare it with a CSV file that contains only the same input that I'm trying to compare which is "gaber123".
For some reason, the output is "fail".
Here is the code
#include <string.h>
#include <stdio.h>
int main() {
char x[20],data[20];
scanf("%s",&x);
FILE *fp= fopen("Accounts.csv","r");
fgets(data,20,fp);
int compare= strcmp(data, x);
if(compare==0){
printf("success!");
}
else{
printf("fail");
}
return 0;
}
Related
I am trying to read a file that is like
91242 FirstName1_LastName1 3.02
I try to use fscanf to read the file and I am confused how to read the second element that with a mixture of character and int. I'm just using one statement to read for test only. I will modify it to a while loop to read the whole text file.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "linked_list.h"
int main() {
node *list_head = NULL;
FILE *file;
file = fopen("student.txt", "r");
if (file == NULL) {
printf("Can not find the file.\n");
}
else {
printf("File students.txt opened successfully.\n");
printf("Linked list built from file students.txt successfully\n");
}
student s;
fscanf(file,"%d%s%lf", &s.id,s.name,&s.gpa);
insert_last(&list_head, s);
print_list(list_head);
}
Here is what i got when I print it out.
91242 FirstName1_LastName1)\�#�4� 3.02
Here is my student.c document
#include "student.h"
void print_student(student student_record){
printf("%d\t%s\t%.2f\n", student_record.id, student_record.name,
student_record.gpa);
}
And here is the definition of student.
typedef struct{ int id; char name[20]; double gpa; } student;
I am working on a program that reads a txt file line by line and copies each line into an empty text file. First the program reads characters until the end of line, and stores all characters read in a buffer. After that it writes the buffer to a text file, and repeats this process until it reaches the end of file. The problem that I'm facing is: the program writes only the first line in the empty file. Can you help to solve this problems please, I'm a newbie in c.
this content txt file i want to read:
hey everyone
how are you
goood
and this the output of the programme:
hey everyone
this the Source code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int GetFileLenght(FILE*FP);
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c;
int i=-1;
int x=1;
char buffer[350];
int lenght;
lenght=GetFileLenght(SourceFile);
printf("%d",lenght);
SourceFile=fopen("D:/SourceFile.txt","r");
while(i!=lenght)
{
c=fgetc(SourceFile);
printf("%c",c);
switch(c)
{
case '\n' :
DestinationFile=fopen("d:/des.txt","a+");
fputs(buffer,DestinationFile);
*buffer=0;
fclose(DestinationFile);
break;
default:
i++;
buffer[i]=c;
printf("%s",buffer);
}
}
return 0;
}
int GetFileLenght(FILE*FP)
{
FP = fopen("d:/SourceFile.txt", "a+");
fseek(FP, 0, SEEK_END);
int lengthOfFile = ftell(FP);
fclose(FP);
return lengthOfFile;
}
EDIT:
I have Made some changes to the program ,now it's good:
I'am also wondering if there is another way to add new line to file using fputs ?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c=0;
int i=-1;
int x=1;
char buffer[350];
SourceFile=fopen("D:/SourceFile.txt","r");
DestinationFile=fopen("d:/des.txt","a+");
while(x)
{
c=fgetc(SourceFile);
switch(c)
{
case '\n' :
fputs(buffer,DestinationFile);
fprintf(DestinationFile,"\n");
printf("%s",buffer);
strncpy(buffer, "", sizeof(buffer));
i=-1 ;
break;
case EOF :
fputs(buffer,DestinationFile);
x=0;
default:
i++;
buffer[i]=c;
}
}
fclose(DestinationFile);
return 0;
}
I am trying to write a program for some classwork that reads in a file using fscanf, and then stores each word into an array with one word in each element. Then I need to print each element of the array out on to a new line on the console.
The getty.txt file has the Gettysburg address in it with appropriate spacing, punctuation, and is multiline.
What I think is happening is that the entire text is being stored in the first element of the array, but I am not 100% sure as I am still learning to debug and write in C.
Any advice as to what I am doing wrong would be great! I currently only seem to be getting the last word and some extra characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void readFile();
void writeFile(char* buffer, int bufferlen);
FILE *fpread;
FILE *fpwrite;
char filebuffer[1000];
int filebufferlen = 0;
int main(int argc, char *argv[]) {
fpwrite = fopen("csis.txt", "w");
readFile();
writeFile(filebuffer, filebufferlen);
fclose(fpwrite);
return 0;
}
void readFile() {
char c;
filebufferlen = 0;
if(!(fpread = fopen("getty.txt", "r"))){
printf("File %s could not be opened. \n", "getty.txt");
fprintf(fpwrite,"File %s could not be opened. \n", "getty.txt");
exit(1);
}
while (!feof(fpread)) {
fscanf(fpread, "%s", filebuffer);
filebufferlen++;
}
}
void writeFile(char* filebuffer, int filebufferlen) {
for (int i = 0; i < filebufferlen; ++i){
printf("%c\n", filebuffer[i]);
}
}
After fixing the compile problems:
the code does not contain any code nor data declarations to contain an array of 'words.' So naturally, nothing but the last word is actually saved so it can be printed out.
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();
}