Problems with file input/output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hodge4ever
    New Member
    • Nov 2007
    • 4

    Problems with file input/output

    Just to give you some background, I am trying to write an encryption algorithmn, so the options are -e to encrypt or -d to decrypt. During the encryption process an array of characters is written to a file called asciivalue.txt and stored for later use. This part of the program appears to work fine, HOWEVER when I try to read back from the file during the decryption process the input I am getting is not what I expected. The input file contains "YYYYYNYYYY Y" but when I read from the file into the array (chartestarray) it appears as random characters i.e. smilie faces, spaces etc......

    I have read other inputs/outputs in the same program using the same logic and coding and and they work perfectly, so I am really confused why this isn't working!?!

    The way I am doing it at the moment is:
    Code:
    FILE *asciivalue_file;
    asciivalue_file = fopen("asciivalue.txt","w");
    fseek(asciivalue_file, 0, SEEK_SET);
    char chartestarray[charcount];
    //Charcount is devised elsewhere and works fine
    
    int readin = 2;
    int i = 0;
    
    while (readin != 7)
    //Very crude way of repeating I know
    {
         lc = fgetc(asciivalue_file);
         if (lc == EOF)
              break;
         else
              if (lc != EOF)
              {
                   chartestarray[i] = lc;
                   i++;
              }
    }
    Hope that makes sense :(
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    After taking a fast look, should not you be opening the file in read mode rather than write mode?
    Code:
    FILE *asciivalue_file;
    asciivalue_file = fopen("asciivalue.txt","w");//<--- should be "r"
    Also, when reading the characters and saving them into the array of characters, you should add the end of string.
    Code:
    i=0;
    while (i != 7)
    {
         lc = fgetc(asciivalue_file);
         if (lc == EOF)
              break;
         chartestarray[i++] = lc;
         chartestarray[i]=0;
    }

    Comment

    • Hodge4ever
      New Member
      • Nov 2007
      • 4

      #3
      Many thanks, your first point pointed me in the right direction.

      I was opening asciivalue.txt using write because if the user chooses -e (to encrypt) then an array of characters is written to that file. If they choose -d (to decrypt) then the characters are read from the file to an array of characters.

      I was declaring this at the start of the program along with my variables, I assumed that if I opened it as write then it would also read i.e. work O.K in both functions. I'm guessing that opening with WRITE access does not necessarily give the user READ access?

      I have now ameneded the code so that it the user chooses -e then asciivalue.txt is opened with write access, however if they choose -d then asciivalue.txt is opened with read access. It is now functioning as expected.

      Comment

      • Cucumber
        New Member
        • Sep 2007
        • 90

        #4
        Correct, if using "w" you can only write, is using "r" you can only read. I dont know whether there is a way to open files in Read and Write mode using C standard file functions.

        Win32 file functions allow opening files in read AND write mode though.

        Comment

        Working...