Thread: read character from file

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    read character from file

    Hi,

    Any ideas on how to read characters from a .txt and then display them on the screne?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try to do some research before you ask:
    http://www.cprogramming.com/tutorial/cfileio.html

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    did that

    first of all i did my recearh BEFORE i asked.

    This is my problem

    Code:
    do {
    infile1=fopen("encrypted.txt", "r");
    fscanf(infile1, "%s", &output);
    //printf("lol");
    printf("%c", output);
    }
    while(output != EOF); // check to make sure user has input
    The problem is that it keeps on printing the first chacter over and over again on the screne.

    Can anyone see the problem with the code?

    BTW: Its my first semester taking C, hence my name.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you keep opening the file each time around the loop.

    Code:
    int ch;
    infile1=fopen("encrypted.txt", "r");
    while ( ( ch = fgetc(infile1) ) != EOF ) {
      putchar( ch );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    It worked!

    Thanks for your help... since i am still studying C, i was trying to figure out how to do that for a few hours. You saved me a lot of time.

    Now I have another problem.
    I have to read charcter from a .txt file.

    Then i must have some sort of loop that would check for certain characters and replace them with ones that have been predefined by the user using a pointer. (no writing to a file involved yet). Single chatactets only, ie it will not search for a KJ rather for K, then subsitute. Then J, then subsitute.

    I figured out how to allow the user to specify the value of the pointers.

    Now i have to write to modified characters to another text file.


    One more thing, how many people here are computer engineer majors, and how many are computer science?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM