Need Help in Reading Text in C

This is a discussion on Need Help in Reading Text in C within the C Programming forums, part of the General Programming Boards category; Hi All, I am having problem reading text in C. I appreciate any help. Thanks YetBo. Code: /*Using Microsoft Visual ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Question Need Help in Reading Text in C

    Hi All, I am having problem reading text in C. I appreciate any help. Thanks YetBo.
    Code:
    /*Using Microsoft Visual Studio 2006
     Tafe NSW
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    	int Id,Number,Value;
    	char Code,First[20],Last[20];
    //Open the file using read mode
    	FILE *fp = fopen("Myfile.txt","r");
    	printf("Id \tNumber \tFirst \tLast \tCode\tValue\n");
    //Checks if EndOfFile
    	while(!feof(fp))	{
    //Need help in reading the names, doesn't read/display properly ??? 
    		fscanf(fp,"%d,%d,%s,%s,%c,%d",&Id,&Number,First,Last,&Code,&Value);
    		printf("%d\t%d\t%s\t%s\t%c\t%d\n",Id,Number,First,Last,Code,Value);
    	}
    	system("pause");
    
    /* This is my test file <Myfile.txt>
    1,3,James,Bond,a,40
    2,4,Clint,Eastwood,s,52
    3,5,Bart,Simpson,,d,50
    */
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    First, that should be
    Code:
    int main(void)
    Now, on to the real issue. For future reference, it is helpful to explain what is going wrong with your code. Simply stating you have a "problem" is not helpful. I can make a guess, though, and this is it: %s reads as much (non-whitespace) text as it can. That means if you give it a comma, it will read that comma, regardless of what your format string looks like. So your first %s is eating the rest of each line. You can use %[^,] instead of %s; this means "any character except a comma".

    It's also usually a bad idea to loop on feof(). Instead, check the return value of your read function; it will tell you if there's been a problem. In this case, fscanf() returns the number of successful conversions. You expect 6, so if it returns anything else, you've got a problem (or EOF). If you use your code as it stands, it will appear to process the last line twice, which your probably don't want. Actually, if that double comma in the last line is really in your data file, it will loop endlessly. Neither of these occurrences would happen if you looped on the return value of fscanf().

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A string (%s) includes the comma, so you can guess what happened.

    To match everything except the comma (ie, so up to the comma) you could use:
    %[^,]
    Since a single name is all characters, no spaces, no numbers (but maybe a hyphen) you could also use:
    %[a-zA-Z-]
    Notice the - at the end. Read your docs for an explanation of how %[] and [^] work.

    And listen to cas .
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Thanks for the quick reply cas and MK27.

    Sorry it's my first time to post.

    Below is the problem I encounter in my program.
    1.) Problem reading text/name ... Resolved now thanks to both of you.
    2.) In Id#2 the code 's' is not aligned.
    3.) Last line displayed twice.
    You were right 'cas' the comma in my file should only be one instead of two.
    I don't know yet to how to return the value for fscanf(). I will search how to return the value.

    Thanks again to both of you,
    YetBo

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The return value from fscanf(), works just like the return value of any C function:

    Code:
    scanNum = fscanf(" whatever...", whatever variables);
    //scanNum now has the return value from fscanf().
    The problem with using tabs for print output is that it relies on the monitor or printer or other software (maybe Notepad), to determine what the "length" of the change in columns, will be. Although 5 to 8 columns is common, it also depends on the built in "hot zones" for that device.

    I'd suggest using just spaces, to get the output you want, and removing the tabs. They're nice, but also unreliable. Your output will look perfect on one program or device, but raggedly on another one. With spaces, it will look great on any device that can handle the type of font you set it up for (proportional or fixed width).
    Last edited by Adak; 10-15-2009 at 09:10 PM.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Wink

    Code:
    My program is now working except for the cosmetic of alignment.
    Thank you guys.
    Below is my latest program for those who are interested:
    /*Using Microsoft Visual Studio 2005
    Program to read records(ie. number,text) in a file
    with the help of 'cas', MK27' and 'Adak' 
    Also notes I read by 'qusah'... Thank you all. YetBo
    */
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    	int Id,Number,Value,Test;
    	char Code,First[20],Last[20];
    //Open the file using read mode
    	FILE *fp = fopen("Myfile.txt","r");
    	printf("Id  Number   First        Last          Code        Value\n");
    //Checks if EndOfFile
    	if(fp)
    	{
    		while(fscanf(fp,"%d,%d,%[^,],%[^,],%c,%d",&Id,&Number,First,Last,&Code,&Value)==6)
    //or	while(fscanf(fp,"%d,%d,%[a-zA-Z-'],%[a-zA-Z-'],%c,%d",&Id,&Number,First,Last,&Code,&Value)==6)
    		{
    		printf("%d   %d        %s        %s          %c           %d\n",Id,Number,First,Last,Code,Value);
    		}
    		fclose(fp);
    	}
    	system("pause");
    }
    /* ===================================
    Myfile.txt
    1,3,James,Bond,a,40
    2,4,Clint,Eastwood,s,52
    3,5,Bart,Simpson,d,50
    4,6,Ronald,Mac-Donald,a,35
    5,7,Neil,O'Bryan,s,66
    =====================================*/
    [/code]
    Last edited by YetBo; 10-15-2009 at 10:57 PM. Reason: so can read program clearly

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your alignment can be improved by using the set field feature of printf().

    Try this:
    Code:
    printf("     %4d\n     %4d", 1,100);
    The 4 means that the field width for the numbers will be set to 4 char's width, no matter what the size is of the number. Really helpful, and you can change the 4 to any size you want, to improve difficult alignment problems.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Thanks Adak.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are very welcome, YetBo.

    I tried to work out your problem with the commas - but I fell asleep at the keyboard.

    Glad it's all working for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text
    By Anator in forum C++ Programming
    Replies: 33
    Last Post: 01-30-2008, 11:13 AM
  2. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 11:27 AM
  3. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 11:51 AM
  4. Reading text file and structuring it..
    By Killroy in forum C Programming
    Replies: 20
    Last Post: 11-19-2004, 07:36 AM
  5. Reading Tab Separted Text files
    By Cathy in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 09:28 AM

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