Thread: storing text file contents into a string

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    storing text file contents into a string

    I've wrote a program that takes a text file in as a string, and encrypts it, using my own, very weak, encryption key. The program works, as long as the text file contains only a single word, with no spaces. I'm assuming for some reason it doesn't like spaces. My code is as follows

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main (int argc, char *argv[])
    {
      FILE *in;
      FILE *out;
      char buf[512];
      char secret[512];
      int x;
       
      in = fopen ( argv[1], "r" );
      out = fopen (argv[2], "w+" );
    
      if ( in == NULL ) {
        perror ( "Unable to open the file" );
      }
    
      /* Read a string */
      fread ( buf, 1, sizeof buf, in );
      printf ( "%s\n", buf );
    
      /*pre programm processesing and beautifying*/
      system("clear");
      printf("\n\n");
    
    
       for (x = 0; x < 512; x++) { //begin for loop
       switch(buf[x])
       {  //begin switch/case
       
       // encryption key hidden for security reasons
    
       }  //end switch/case
       }  //end for loop
    
    
    
      fprintf(out, "%s\n", secret);  
      fclose ( in );
      fclose (out);
    
    
      printf("%s\n", secret);
    
    
    
       printf("\n\n");
    } //end main
    If anyone could recommend any modifications, I'd appreciate it.
    Michael

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Ahh, you have to love security by obscurity. I don't see why it would only handle single words with the code you've shown. Are you sure your encryption isn't changing a space into '\0'?

    When you print the string right after reading it does it print the whole string or only 1 word? Please explain what you mean by "it only works when it's 1 word".

    And also:
    Code:
    for (x = 0; x < 512; x++) { //begin for loop
    Gee, ya think?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    20
    ok, by "it only works with one word", i mean, if i use a line of text that includes a space, the text file created by the program, is completely empty.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    20
    ok, i have that part figured out, but now i have a new question. When I store the text file into a string, I think that setting the string to 512 bytes, it automatically fills in the rest of the 512 bytes if the text file does not fill the buffer. Is there a way to do a SIZEOF file.txt? or something to that end?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why not use the return value of fread()? It tells you how many items it read.
    You could do:
    Code:
    {
      size_t data_len;
    
      data_len = fread ( buf, 1, sizeof buf, in );
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. search for text string in a file
    By basenews in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2007, 05:15 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM