Reading Character at a time from a text file

This is a discussion on Reading Character at a time from a text file within the C Programming forums, part of the General Programming Boards category; Hi everyone, this is my first post so be nice!! I want to read from a text file and output ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    Reading Character at a time from a text file

    Hi everyone, this is my first post so be nice!!

    I want to read from a text file and output single characters to an external device. I can access the file, read the whole string, and output the whole string to the device. But I really need to do it character at a time in a wee loop.

    Here's the code I have so far;
    ---------------------------------------------------------------------------------
    Code:
    	if((textfile = fopen("temp.txt","r"))==NULL){
    		printf("Error opening text file for reading\n");
    		exit(0);
    	}
    
    	while((fscanf(textfile, "%s", line)!=EOF))
                    {
    		
    		WriteFile(hCom, line, 1, &temp, NULL);
    	}
    
    	fclose(textfile);
    ---------------------------------------------------------------------------------

    What it is doing is opening the file for reading and if it can't it'll output an error message. The it scans the file and outputs through the COM port.

    The problem I am having is that I can't split the string up into individual characters. When I output the whole string to the device it, obviously, only receives the first character since I'm only sending a byte at a time.

    Any help in splitting the string up would be greatly appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    instead of scanf() use fgetc() , which gets only one char from the file.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    are you meaning to just change the line

    Code:
    while((fscanf(textfile, "%s", line)!=EOF)){
    to

    Code:
    while((fgetc(textfile, "%s", line)!=EOF)){
    ?

    When I do that I get warnings saying;

    warning C4020: 'fgetc' : too many actual parameters.

    I should also say, even though it's only a warning, the external device receives no characters when I put fgetc() in.
    Last edited by Giania; 02-25-2006 at 08:39 AM.

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    You want something like this:

    Code:
      int ch;
      ...
      while ((ch = fgetc (textfile)) != EOF) {/* do your thing */}
    You need to find a good reference so you can look up the standard functions and see how to use them.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    [QUOTE=Giania]are you meaning to just change the line

    Code:
    while((fscanf(textfile, "%s", line)!=EOF)){
    to

    Code:
    while((fgetc(textfile, "%s", line)!=EOF)){

    didn't you read that link I posted? It shows you how to use that function.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    There's something important in eerok's post:
    Code:
      int ch;
      ...
      while ((ch = fgetc (textfile)) != EOF) {/* do your thing */}
    EOF is an int value, not a char one, so you have to make ch an int to test against it. See the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Thanks for that eerok. You mean like the Visual C++ help file? :P

    I had a look and got it working. I'm not a programmer so my programming skills aint the best.

    Anyway, here's what I did;

    Code:
    for(count=0;(count<size_of)&&((ch = fgetc (textfile)) != EOF)&& (ch != '\0');count++) 
    {
          buffer[0] = (char)ch;
    	WriteFile(hCom, buffer , 1, &temp, NULL);  //write to the COM port.
    	for (i=0;i<100000000;i++)  //just a delay so you can see the LEDs flashing
    		{
    		}
    }
    Thanks to all.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    	for (i=0;i<100000000;i++)  //just a delay so you can see the LEDs flashing
    		{
    		}
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    BTW, 100,000,000 is not guaranteed to fit into an int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Quote Originally Posted by Ancient Dragon


    didn't you read that link I posted? It shows you how to use that function.
    Lol, no, I never noticed that that was a link. I thought you had just coloured the text to make it stand it!!! lol

    I did say I was new here!



    Thanks for that Dwks. My code is still at the developing stage and still needs loads of tidying up. I'll keep the sleep function in mind when I come to write it so it works on any machine. Since the delay on my machine won't guarantee the same delay on others.

    Thanks again
    Last edited by Giania; 02-25-2006 at 02:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 10:37 AM
  4. Reading output into a text file
    By pete212 in forum C Programming
    Replies: 8
    Last Post: 04-23-2008, 05:11 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

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