Need help on displaying characters

This is a discussion on Need help on displaying characters within the C Programming forums, part of the General Programming Boards category; I need to be able to display every other character in my test.dat file. The test.dat file contains the number ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Need help on displaying characters

    I need to be able to display every other character in my test.dat file. The test.dat file contains the number 123456.
    What is the best way to go about this?


    #include<stdio.h>
    int main()
    {
    int ch;
    long int offset, last;
    FILE *inFile;

    inFile = fopen("test.dat","r");
    if (inFile == NULL)
    {
    printf("\nFailed to open the test.dat file. \n");
    exit(1);
    }
    fseek(inFile,0L,SEEK_END);
    last = ftell(inFile);
    for(offset = 0; offset <= last ; ++offset)
    {
    fseek(inFile, +offset, SEEK_SET);
    ch = getc(inFile);
    switch(ch)
    {
    case '\n': printf("LF : ");
    break;
    case EOF : printf("EOF: ");
    break;
    default : printf("%c : ",ch);
    }
    }
    fclose(inFile);

    return 0;
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    First of all, use code tags. Second, what you want to do? print all the file? char by char?

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *fp;
        unsigned long i;
        int c;
    
        fp = fopen("input.txt", "r");
        if (fp == NULL)
        {
            perror("fopen failed");
            exit(EXIT_FAILURE);
        }
    
        for (i = 0; (c = fgetc(fp)) != EOF; i++)
        {
            /* Skip even positions */
            if (i % 2)
                continue;
    
            putchar(c);
        }
    
        return 0;
    }
    p.s. What the alphabet would look like without q and r.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Thanks for the help

    I used this part of the code and it works very well. Thanks.
    Code:
    	for(offset = 0; offset <= last ; ++offset)
    	{
    		if (offset % 2)
    			continue;
    Last edited by chadwickba2; 04-12-2003 at 01:35 PM.

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    for(offset = 0; offset <= last ; ++offset)
    This is very likely an off-by-one bug. By starting at 0 and going until offset is greater than last, you're actually looping last+1 times. You probably don't want to do that, so you'd have to change the loop to this
    Code:
    for(offset = 0; offset < last ; ++offset)
    p.s. What the alphabet would look like without q and r.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Thank you for all the help. That line of code actually came out of the C Programming textbook that I am using. The book is not too bad, but has several mistakes in it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. displaying hex characters
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 02:18 PM
  3. Problem displaying characters
    By caduardo21 in forum C Programming
    Replies: 2
    Last Post: 05-24-2005, 06:51 AM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  5. French characters not displaying on MS DOS Window
    By CookieMonster in forum C Programming
    Replies: 21
    Last Post: 04-05-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21