john clarke
john clarke

Reputation: 11

I need help with a while loop in a loop

I need help converting a code to legible form so that each line of code has the legible form underneath its resspective line. Im reading from a file with a contiguous line of txt which is to be decoded. I know i need a while loop to find the end of line character but im not quite sure how to write it. My code is below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


main()
{
    char* ptr;              /* Declare a pointer */
    int i;int shift = -2 ;
    int STR_LENGTH;
    int SIZE;               /* Declare our variables */
    char mystring [150];    /* Declare a string */

    FILE * pFile;

    pFile = fopen ("myfile.txt" , "r");
    if (pFile == NULL)
        perror ("Error opening file");
    else
    {
        fgets (mystring , 150 , pFile);
        puts (mystring);

        STR_LENGTH=strlen(mystring); /* Get the string length */
        SIZE=sizeof(mystring);       /* Get the sizeof the string */

        /* Print out the values */
        printf("\n the value in strlen is %d size is %d\n",STR_LENGTH,SIZE);
        /* Point the pointer at the first element */ 
        ptr = &mystring[0];

        for (i = 0; i < STR_LENGTH; i++) /* Set up the loop */
        {
            /* Shift the first character in the string */
            *ptr = mystring[i]+shift;

            ptr++;                          /* Increment the counter */
        }
        printf("The code is!!\n");          /* Print out the result */
        printf("%s",mystring);

        fclose (pFile);
    }
}

The finished program should print like this

K"jcxg"c"oqwvj"."K"fq"pqv"urgcm
i have a mouth i do not speak
K"jcxg"hqwt"g{gu"."dwv"ecppqv"ugg
i have four eyes but cannot see
K"jcxg"c"dgf"."dwv"fq"pqv"unggr
i have a bed but do not sleep
Ecp"{qw"vgnn"og"yjq"K"dgA"
you tell me who i be?

instead it reads like this with the existing code

K"jcxg"c"oqwvj"."K"fq"pqv"urgcmK"jcxg"hqwt"g{gu"."dwv"ecppqv"uggK"jcxg"c"dgf"."dwv"fq"pqv"unggrEcp"{qw"vgnn"og"yjq"K"dgA"
i have a mouth i do not speak
i have four eyes but cannot see
i have a bed but do not sleep
you tell me who i be?

im using turboC Any help will be much appreciated!

Upvotes: 1

Views: 317

Answers (3)

pmg
pmg

Reputation: 108978

Not an answer, so community wiki.

I believe this recreates the input file like the one used by the OP:

#include <stdio.h>
/* #define FILENAME "myfile.txt" */
#define FILENAME "5224516.txt"

int main(void) {
  FILE *f;
  char data[] = {
    75,34,106,99,120,103,34,99,34,111,113,119,118,106,34,46,34,75,34,
    102,113,34,112,113,118,34,117,114,103,99,109,12,75,34,106,99,120,
    103,34,104,113,119,116,34,103,123,103,117,34,46,34,100,119,118,
    34,101,99,112,112,113,118,34,117,103,103,12,75,34,106,99,120,103,
    34,99,34,100,103,102,34,46,34,100,119,118,34,102,113,34,112,113,
    118,34,117,110,103,103,114,12,69,99,112,34,123,113,119,34,118,
    103,110,110,34,111,103,34,121,106,113,34,75,34,100,103,65,
#ifdef _WIN32
    13,
#endif
    10
  };

  f = fopen(FILENAME, "wb");
  if (f == NULL) {
    perror("file open");
  } else {
    size_t n;
    n = fwrite(data, 1, sizeof data, f);
    if (n != sizeof data) perror("file write");
    if (fclose(f)) perror("file close");
  }

  return 0;
}

Upvotes: 1

bta
bta

Reputation: 45057

The fgets and puts functions don't know that your newlines are encoded so they don't know where to break the strings. This is why your output shows the entire encoded input on one line before any of your decoded output.

One way to solve this is to add normal newline characters into the encoded string before printing it out. Another similar option is to manually walk the input string, find the encoded newlines, and break the input string apart into four separate strings (that you can convert and print out individually).

The code you posted doesn't produce the output you posted, so it's hard to give you any specific code suggestions. Please provide a working, compilable code sample and the output it produces and we will be able to provide you with more useful feedback. You can solve this without nesting a loop within another loop (but the code you posted only has one loop in it so it's difficult to see where your second loop is coming from).

Upvotes: 1

ThomasMcLeod
ThomasMcLeod

Reputation: 7769

Try this:

i = 0;
ptr = & mystring[0];
while (i < STR_LENGTH)
{
     * ptr = mystring[i] + shift;
     if (* ptr = '\n')
     {
         printf("%s", mystring);
         i = STR_LENGTH;
     }
     else
     {
        i++;
        ptr++;
     }
} 

Edit: this assumes that you will have an outer loop that is loading mystring with new lines of cipher text. (Sorry about incorrect loop terminator in first version)

Upvotes: 0

Related Questions