Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 C: Read from file and write to file

View previous topic View next topic Go down 
AuthorMessage
DayLight
Member
Member
DayLight

Posts : 6
Join date : 2011-06-05

C: Read from file and write to file Empty
PostSubject: C: Read from file and write to file   C: Read from file and write to file EmptyWed Jun 29, 2011 4:01 am

Hello!

I have a little problem with my code, I have been programming C for a couple of months now, and I need some help on this one I want to read a string from a file called Test.txt. Inside the file there is a string "Hello World!". And if that string is alredy there, the program should return 0 from the function read_file to read2, but it always returns 1, and in that way, it always writes "Hello World!" again to the file, which is not what I want..

#include <stdio.h>
FILE *file;

int read_file(char read [30]) {

file = fopen ("Test.txt", "rt");
fscanf(file, "%s" , read);
if (read == "Hello World!")
return 0;
else
return 1;
fclose(file);
}

int main (void) {
char read1 [30];
int read2;
read2=read_file(read1);
file = fopen ("Test.txt", "wt");
printf("%d \n", read2);
if (file == NULL)
printf("Can't open file. \n");
else if (read2 == 0)
printf ("That was alredy inside the file, closing...\n") && fclose(file);
else if (read2 == 1)
fprintf(file, "Hello World!") && printf ("Successfully written to the file. \n") && fclose(file);
getchar();
}
Back to top Go down
Zoe_Baby
Member
Member
Zoe_Baby

Posts : 17
Join date : 2011-06-05

C: Read from file and write to file Empty
PostSubject: Re: C: Read from file and write to file   C: Read from file and write to file EmptyWed Jun 29, 2011 4:02 am

Code:

read == "Hello World"

This is not the correct way to compare strings. Use strcmp.
Back to top Go down
DayLight
Member
Member
DayLight

Posts : 6
Join date : 2011-06-05

C: Read from file and write to file Empty
PostSubject: Re: C: Read from file and write to file   C: Read from file and write to file EmptyWed Jun 29, 2011 4:03 am

Thank you so much for taking your time, but I don't understand how I would use it. I would be very glad if you could show me an example of how I would go with it
Back to top Go down
Zoe_Baby
Member
Member
Zoe_Baby

Posts : 17
Join date : 2011-06-05

C: Read from file and write to file Empty
PostSubject: Re: C: Read from file and write to file   C: Read from file and write to file EmptyWed Jun 29, 2011 4:04 am

The link I provided has examples on the page. But here is how you would check to see if two strings are equal. strcmp is case-sensitive; it continues until a null character is reached in one of the strings.

Code:

if (strcmp(read, "Hello, World!") == 0) // Checks to see if the strings are equal
  return 0;
return 1;

Also, your fclose should come before your return statement, otherwise it will never be executed. And in your main function, return 0 at the end to signal a successful exit. This value is sometimes used by the operating system.
Back to top Go down
DayLight
Member
Member
DayLight

Posts : 6
Join date : 2011-06-05

C: Read from file and write to file Empty
PostSubject: Re: C: Read from file and write to file   C: Read from file and write to file EmptyWed Jun 29, 2011 4:05 am

Okay, thank you very much :D
Back to top Go down
Sponsored content




C: Read from file and write to file Empty
PostSubject: Re: C: Read from file and write to file   C: Read from file and write to file Empty

Back to top Go down
 

C: Read from file and write to file

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» where to write a script
» Seeing If File Is Audio
» file size help
» Please help - Dom Parsing XML file to HTML?
» Giving link to html file through xml

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: C: Read from file and write to file Edit-trash Useless :: Trash-