4 Replies - 354 Views - Last Post: 10 April 2012 - 04:02 PM Rate Topic: -----

#1 Lej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 12

Easy file I/O question.

Posted 10 April 2012 - 11:26 AM

I have to make a program that opens input.txt, encrpyts it, then outputs it to output.txt.

I enter the key, which means if they key is 1, all letters get shifted up 1 letter. (To B). And Z becomes A.

Well, I've got all that figured out and working, which my code will show.. But I don't understand how to scan in strings with spaces... Or even use File I/O for that matter. I can open it and read it, and I tried using fgets to save it to a variable, (message).. but it only got the first line of the text.


/////////////////////////
input.txt:
This is the input for
the encryption program
/////////////////////////
		
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string>


void main(){

	/* Variables */
	int i;
	int shift;
	char message[50]="0";


	/* Get key and message */
	printf("Enter the key: ");
	scanf("%d", &shift);
	printf("\nMessage: ");
	scanf("%s", message);

	/*FILE *fp;
	fp=fopen("input.txt", "r");

	if (fp != NULL){
		 scanf("%s",message);
	}

	else printf("Error: Cannot open file");*/



	/* Make all letters uppercase */
	for(i=0; message[i]; i++)
		message[i] = toupper(message[i]);


	/* Convert based on key */
	for(i=0; message[i]; i++)
		if(message[i]!= ' ') 
		   message[i]=((message[i]-'A') + shift) % 26 + 'A';


	/*Print encrypted message */
	printf("\nEncrypted: ");
	for(i=0; message[i]; i++)
		printf("%c",message[i]);
	printf("\n\n");

}



How do I read the spaces and line breaks? I want to replace ALL spaces in input.txt with line breaks, along with all the spaces to line breaks as well.

This post has been edited by Lej: 10 April 2012 - 11:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Easy file I/O question.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Easy file I/O question.

Posted 10 April 2012 - 11:40 AM

Use fgets.
Was This Post Helpful? 0
  • +
  • -

#3 Lej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 12

Re: Easy file I/O question.

Posted 10 April 2012 - 11:45 AM

Alright, so here's my new code.

		
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string>


void main(){

	/* Variables */
	int i;
	int shift;
	char message[50]="0";


	/* Get key and message */
	printf("Enter the key: ");
	scanf("%d", &shift);


	/* Open input.txt and save it to message variable */
  FILE * fp;
  fp = fopen ("input.txt" , "r");
   if (fp == NULL) perror ("Error opening file");
   else {
     if ( fgets (message , 1000 , fp) != NULL )
       puts (message);
     fclose (fp);
   }



	/* Make all letters uppercase */
	for(i=0; message[i]; i++)
		message[i] = toupper(message[i]);


	/* Convert based on key */
	for(i=0; message[i]; i++)
		if(message[i]!= ' ') 
		   message[i]=((message[i]-'A') + shift) % 26 + 'A';


	/*Print encrypted message */
	printf("\nEncrypted: ");
	for(i=0; message[i]; i++)
		printf("%c",message[i]);
	printf("\n\n");

}



Which ALMOST works, but with a key of 0, outputs this:

Encrypted: THIS IS THE INPUT FOR>
Press any key to continue . . .

It's basically breaking at the line break. (Putting a >)

This post has been edited by Lej: 10 April 2012 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Lej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 01-February 12

Re: Easy file I/O question.

Posted 10 April 2012 - 12:06 PM

http://i41.tinypic.com/2nhi748.png

Why is it doing that garbage? Storing half of it in message, and the rest in fp....

This post has been edited by Lej: 10 April 2012 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1362
  • View blog
  • Posts: 4,715
  • Joined: 19-February 09

Re: Easy file I/O question.

Posted 10 April 2012 - 04:02 PM

Instead of testing for a space you can test for an alphabetic character using the isalpha functiion from ctype.h.


  /* Convert based on key */
  for(i=0; message[i]; i++)
    /* if(message[i]!= ' ') */
    if(isalpha(message[i]))
      message[i]=((message[i]-'A') + shift) % 26 + 'A';


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1