Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Read to files help!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nikola




PostPosted: Sun Jun 22, 2008 11:30 pm   Post subject: Read to files help!

Alright im just as confused, if not more with reading files. Not so much reading but more with printing out the files contents! Here is my code so far (Some parts are deleted to ensure no one steals it). The parts with <--- are what i cant manage to get to work! Please help!

code:
int main()
{
      FILE *file;
      FILE *file2;
      FILE *file3;
      file = fopen("c:\\Users\\Nikola\\Desktop\\email.dat", "w");
      file2 = fopen("c:\\Users\\Nikola\\Desktop\\emailmark.dat", "r");
      file3 = fopen("c:\\Users\\Nikola\\Desktop\\emailjohn.dat", "r");
      char c[10];
      char str_username [40];
      char str_password [40];
      char str_sendmail[400];
      char str_mailto[50];
      char str_subject[50];
      int int_ctr = 0;
      int int_options = 0;
      int int_options2 = 0;
      int int_options3 = 0;
     
      while(int_ctr <= 1)
{
      printf("Email Login\n");
      printf("Username: ");
      scanf(" %s", str_username);
      printf("Password: ");
      scanf(" %s", str_password);
      system ("CLS");
      if(!strcmp(str_username,"admin") && !strcmp(str_password,"admin"))
{
         printf("Welcome\n");
         int_ctr = 2;
         printf("What would you like to do first?\n");
         printf("1. Read Inbox\n2. Send Email\n");
         scanf(" %d", &int_options);
}
      else
         printf("Wronge Password! \nTry Again\n\n");
}
      system ("CLS");
      if(int_options == 1)
{
         printf("From:\t\tSubject:\n");
         printf("1. Mark\t\tMeet Up?\n2. John\t\tHey!\n");
         printf("Which would you like to read first: ");
         scanf(" %d", &int_options3);
}
      if(int_options3 == 1)
      fprintf(file2, "Email: %s", c);
      if(int_options3 == 2)
      printf("Email: Whats up? Im bored!\n");
      if(int_options == 2)
{
         printf("To: ");
         fflush(stdin);
         gets(str_mailto);
         printf("Subject: ");
         fflush(stdin);
         gets(str_subject);
         printf("Please enter email: ");
         fflush(stdin);
         gets(str_sendmail);
         system ("CLS");
         printf("Email Sent!\n");
         printf("What would you like to do now?\n1. Back To Menu\n2. Logout\n");
         scanf(" %d", &int_options2);
}
      if(int_options2 == 1) <--------------------------------------------------------------------------------------------------------------
         printf("not done yet\n");<---------------------------------------------------------------------------------------------------------
      if(int_options2 == 2)
{
         system ("CLS");
         printf("Closing");
         Sleep(400);
         printf(".");
         Sleep(400);
         printf(".");
         Sleep(400);
         printf(".\n");
         Sleep(400);
         printf("GoodBye!\n");

}
      fprintf(file, "To: %s\nSubject: %s\nEmail: %s", str_mailto, str_subject, str_sendmail);
      if (file == NULL)
{
         printf("I couldn't open email.dat for writing.\n");
         exit(0);
}

      system("PAUSE");
      return 0;
}

Sponsor
Sponsor
Sponsor
sponsor
jernst




PostPosted: Mon Jun 23, 2008 9:26 am   Post subject: Re: Read to files help!

why dont you try printing out what is in the variable to see what the value actually is to debug the code
Nikola




PostPosted: Mon Jun 23, 2008 10:00 am   Post subject: RE:Read to files help!

Not too sure i understand what you mean. If you are suggesting for me to print out the variable "c" just to see if it holds any values than im not too sure that thats where my problem lies. I have a dat file or txt file and i just want it too print the contents of that file in the code.
jernst




PostPosted: Mon Jun 23, 2008 11:24 am   Post subject: Re: Read to files help!

well according to your arrows in the code the part with
code:
if(int_options2 == 1)
is not working so why not print out that variable and see what is in there so you can see why your if statement isn't working the way you want
Nikola




PostPosted: Mon Jun 23, 2008 11:33 am   Post subject: RE:Read to files help!

Its not the if statement. Its the fprint im not sure what im doing in there. I know that part is wronge the if is not. The if is only telling the program, if the person picks one read this file. Im having problems with the actual reading of the files.
btiffin




PostPosted: Mon Jun 23, 2008 12:45 pm   Post subject: Re: Read to files help!

Ok; when programming in C, reading a file and displaying the contents is usually done a line or a character at a time.

I'm getting the feeling that you want to open a file and then just call printf. That's not the way to think about this problem.

You need to open the file stream then start a loop. The loop will read a character, and if not at end of file, then display the character and loop again.

Here is a piece of code I snagged from http://www.lysator.liu.se/c/bwk-tutor.html#simple-io. Written by Brian Kernighan himself, some four years before the famous C white book - this is C from 1974. So I shouldn't be giving away too much, as you will need to plug in current C syntax (fgetc, putc and EOF tests, and c won't be a char, but an int etc...)

C:

       main( ) {
               char c;
               while( (c=getchar( )) != '\0' )
                       putchar(c);
       }


The point is; open the file then read a character, print a character all in a loop until you get to EOF and then close the file.

Cheers
Nikola




PostPosted: Mon Jun 23, 2008 1:27 pm   Post subject: RE:Read to files help!

so putchar(variable) is what will print it out?
btiffin




PostPosted: Mon Jun 23, 2008 1:40 pm   Post subject: RE:Read to files help!

Yep. But putchar is OLD (still supported - but old). Look to fputc(ch, stdout) or the other options.

But remember; this is one character at a time. Not an entire file.

Cheers
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: