Thread: how to read characters from file

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    5

    how to read characters from file

    Hi,

    i'm new with C language. I got task to do . Actually there are 2 points. Both should be done with reading text from file.


    1. write down all letters from text in reverse alphabetic order (each letter 1 time).
    2. write down all words in random order.


    I'm stuck with first point. Below code gives me entire text, but i need unique characters only. Text file contains few sentences.
    Code:
    #include<stdio.h>
    void main( )
    {
    FILE *in;
    int ch;
    if ( (in = fopen("test.txt", "r") ) != NULL )
    
    {
    while( (ch= fgetc(in) ) != EOF )
    fputc(ch, stdout);
    fclose(in);
    }
    else
    printf("File test.txt not open... \n");
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Welcome to the forums.

    The first part asks to enumerate all the available characters, and present them like this:
    Code:
    Text: majestic
    Output: t s m j i e c a
    There are many ways to do so. One of them is to create an array and check that each time you insert a new character, it doesn't already exist, and sort them using Insertion Sort.

    For the second part, you need to store all the words, shuffle them using some algorithm, and then print them back out. Quite simple.

    Right now your code does nothing more than output the text of the file as-is. For starters, try outputting the different characters as you encounter them, but only once each.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Hi,

    thank you for tips. I was trying to find how to put text (letters) into array, but i found only about numbers. Maybe someone could show me how this code should look? Should it be putted into 1D or 2D array?

    Thanks

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    I tried this code, but it gives me nothing.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int i;
        FILE *in = fopen("test.txt", "r");
        if (in) {
            char mas[50];
            size_t n = 0;
            int ch;
            while ((ch = getc(in)) != EOF) {
                mas[n++] = (char)ch;
            }
            fclose(in);
    
    
            }
        for(i=0;i<50;i++)
        {printf("%c",mas[i]);
        printf("\n");}
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rimasbimas
    I tried this code, but it gives me nothing.
    It gives me a warning and an error:
    Code:
    test.c: In function ‘main’:
    test.c:9:14: warning: variable ‘mas’ set but not used [-Wunused-but-set-variable]
             char mas[50];
                  ^
    test.c:20:18: error: ‘mas’ undeclared (first use in this function)
         {printf("%c",mas[i]);
                      ^
    test.c:20:18: note: each undeclared identifier is reported only once for each function it appears in
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Hi,

    i manage to write codes for both tasks. I'm just stuck with one thing - i have string array with words from text. How can i sort them in random way? This is my code to get string array.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        /*declare and initialise variable*/
        char message[10][150],buffer[150];
        int i=0;
        FILE *file_in;
        file_in=fopen("test.txt", "r");
        /*stores and prints the data from the string*/
        while(fgets(buffer,150,file_in)){
            strcpy(message[i],buffer);
            printf("%s",message[i]);
            i++;
        }
    
    
        getchar();
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for "Fisher-Yates shuffle".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Hi, i have this code, but it gives me blank screen when i run it.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    
    
        char message[10][150],buffer[150];
        int i=0;
        int cntr=9;
        int freeArray[9];
        srand(time(NULL));
        for (i=0;i<cntr;i++)
        freeArray[i]=rand()%cntr;
        FILE *file_in;
        file_in=fopen("test.txt", "r");
    
    
        while(fgets(buffer,150,file_in)){
        strcpy(message[freeArray[i]], buffer);
        }
    while(cntr>=0)
     {
    i=rand() % cntr;
    strcpy(message[freeArray[i]], buffer);
      freeArray[i] = freeArray[cntr--];
      printf("%s", message[freeArray[i]]);
     }
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing characters read from text file in 2 different string
    By rcplguy15 in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2010, 04:25 AM
  2. Read the number of characters
    By rehan in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 10:57 AM
  3. read characters
    By modec in forum C Programming
    Replies: 2
    Last Post: 08-19-2003, 11:58 AM
  4. Plz Tell me how to Read Characters like Key UP, Key Down
    By arvindkr in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 06:22 PM
  5. Replies: 4
    Last Post: 06-21-2002, 02:52 PM