Thread: File input question from a newbie

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    File input question from a newbie

    What I'm trying to do is read a simple text file in then encyrpt it,
    for example A will become M, B becomes N, etc, this will be the output. But I can't get it to actually read the file.

    The encyrption works fine, if I just prompt the user to manually enter the text, removing the file opening code.

    Thanks for any help you can give me.




    Code:
    #include <stdio.h>
    
      FILE *fp;
          fp=fopen("c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\cipher.txt", "r");
    
    int
    main( void )
    {
          
          int   ch;
          
          while( (ch = getchar()) != EOF ){
                 if( ch >= 'A' && ch <= 'Z' )
                           ch += 13 ;
                
                 putchar( ch ):
                 
                 }
          
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The '\' character has a special meaning in C strings. In your path, try using '/' instead of use \\ (e.g. c:\\blah\\foo)

    If you'd checked the return value of fopen() like you should you would have realized there was an error when trying to open the file.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    The '\' character has a special meaning in C strings. In your path, try using '/' instead of use \\ (e.g. c:\\blah\\foo)

    If you'd checked the return value of fopen() like you should you would have realized there was an error when trying to open the file.

    I knew there was an error there, I didn't know that '\' was a special character. That part is solved but I these are the errors I'm getting now.

    6 c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\pr-5.c conflicting types for 'fp'

    5 c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\pr-5.c previous declaration of 'fp' was here

    6 c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\pr-5.c [Warning] initialization makes integer from pointer without a cast

    6 c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\pr-5.c initializer element is not constant

    6 c:\Documents and Settings\Mckinstry_MaL\Desktop\My-Projects\pr-5.c [Warning] data definition has no type or storage class

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, you have to actually call fopen() from inside a function. Try moving your fp=fopen(...) line into your main function.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, have you considered what's going to happen when the input character is one of the last 13 letters of the alphabet?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Also, have you considered what's going to happen when the input character is one of the last 13 letters of the alphabet?
    Yeah just now I see what happens when input Z I get lowercase g, input R I get _.

    Should it now be able to read the file fopen is in the main function now, it keeps prompting me to maually enter the text.

    Code:
    int
    main( void )
    {
          FILE *fp;
          fp=fopen("c:/Documents and Settings/Mckinstry_MaL/Desktop/My-Projects/cipher.txt", "r");
          
          
         
          int   ch;
          
          while( (ch = getchar()) != EOF ){
                 if( ch >= 'A' && ch <= 'Z' )
                           ch += 13 ;
                
                 putchar( ch );
                 
                 }
        
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    getchar() reads from stdin (normally the keyboard). If you want to read from the file, use fgetc() and pass it your fp variable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Cool

    Thanks for yours help guys it works now.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Actually just one more quick question. As itsme86 pointed out I'm going to get not exactly the results I'm looking for. What I need is for output to be just letters and the correct one like a to m, z to m, so that for debugging purposes I can easliy copy the out eycrpyted output back in the text file run it, and get the original input.

    I know there's something not quite right with this part.

    Code:
    if( ch >= 'A' && ch <= 'z' )
              ch += 13 ;
    Again thanks for your help

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You want to 'split' your alphabet in half -- if its greater than or equal to 'A' and less than 'N', then add your 13. If its greater than or equal to 'N' and less than 'Z' then subtract your 13, and so on..

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question reguarding safe practice of file io
    By Ashii in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2008, 03:47 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM