Thread: please do my homework!!

  1. #1
    Unregistered
    Guest

    Post please do my homework!!

    I won't lie- this is for homework. I've been scratching my head for a while- can you help?

    I'm working on a simple SSC encryption program. The question I'm stuck on requires you to read a key in from a txt file (which will just simply contain the letters a-z not not in order), assign it to an array, and then display the characters in order of the key on the screen to prove that the array works. I found this bit of code on the board:

    char tempStr[128];
    FILE *fPtr = fopen("NEW.TXT", "r");

    while(!feof(fPtr)) {
    fgets(tempStr, 80, fPtr);
    fprintf(stderr,"%s\n", tempStr);
    }

    Could anyone walk me through these lines of code- I don't understand them. Thanks!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: please do my homework!!

    Code:
    char tempStr[128]; 
    FILE *fPtr = fopen("NEW.TXT", "r"); 
    
    while(!feof(fPtr)) { 
    fgets(tempStr, 80, fPtr); 
    fprintf(stderr,"%s\n", tempStr); 
    }
    This code is wrong. Plain and simple.

    1. Always check that a file pointer is opened successfully before using it.
    2. Don't use feof() in this manner, it doesn't work as you'd expect.
    3. Why limit the input to 80 bytes on the fgets(), when the buffer is 128 bytes long ? Not a problem, just a mismatch.

    There are plenty of other examples around here...... see what you can come up with, then I'll help again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    I'm at the labs at my uni now (don't have working c compiler at home), and put that bit of sample code in. And yes, it does appear to be total bolocks. But! I nearly have my own solution working. I'm having difficulty displaying the output correctly on the screen. So far I have
    Code:
    /***************************************
    PREPROCESSOR DIRECTIVES
    ***************************************/
    #include <stdio.h>
    #include <stdlib.h>
    /***************************************
    FUNCTION PROTOTYPES
    ***************************************/
    
    /***************************************
    FUNCTION main
    ***************************************/
    void main()
    {
    
    	FILE *infp;
    	int c;
    	char key[26];
    
    
    if((infp = fopen("u:\\c_coursework\\key.txt", "r")) == NULL)
        {
            printf("Unable to open the input file\n");
            exit(0);
        }
    
    	while((c = getc(infp)) !=EOF)
    	{
    			fprintf(stderr,"%s", key);
    	}
    	fclose(infp);
    }
    The problem is in the fprintf line obviously. I'm basically trialing and erroring (emphasis on the latter) to get it to work. I got it to display the letters from the textfile above each other with loads of weird squiggles and in on several lines, with loads of weird squiggles in between each letter. urgh!
    so.... could ya correct that line fer me? cheers!

  4. #4
    Unregistered
    Guest
    sorry- just to clarify- i wish to have it display the letters from the text file like this:
    Q W E R T Y U I
    so i need to tell it to print the letters from the array with a space between each character so it looks neat.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >fprintf(stderr,"%s", key);
    should be
    >fprintf(stderr,"%c", key);

    And if you want spaces between the characters, it should be
    >fprintf(stderr,"%c ", key);

    Dont use this either:
    >void main()
    use this instead
    int main(void)
    and make sure you return something at the end of main.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest

    Reply

    try this:



    char tempStr[128];
    int i, count;
    char ch;
    FILE *fPtr = fopen("NEW.TXT", "r");

    count = 0;
    while(!feof(fPtr))
    {
    fgets(ch, 80, fPtr);

    tempStr[count] = ch;

    count++;
    }


    for(i=0;i<128;i++)
    fprintf("%c\n", tempStr[i]);

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Reply

    Originally posted by Unregistered
    try this:
    char tempStr[128];
    int i, count;
    char ch;
    FILE *fPtr = fopen("NEW.TXT", "r");

    count = 0;
    while(!feof(fPtr))
    {
    fgets(ch, 80, fPtr);

    tempStr[count] = ch;

    count++;
    }


    for(i=0;i<128;i++)
    fprintf("%c\n", tempStr[i]);
    Read my first post in this thread..... this code is wrong. Trust me.

    [EDIT] I just read it through again..... it's even wronger than I first thought. [/EDIT]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM