Thread: An imporant question on files and records

  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9

    An imporant question on files and records

    i am trying to access a file EMP.TXT which has a huge list of employee no (empno),employee name (ename), employee's salary (esal) . Can anybody tell me how to retrieve only the ename exculding all other details. I have tried a lot using fscanf (), fread(),fgets(),sscanf() but to no avail.I am able to direct the whole file to another file called EMP1.TXT but i can't retrieve only enames. PLz help !!!!!!!!

    yashpal

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    It depends entirely on how the file is saved.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    when one opens the file you can see

    xxxxxx 1234 2000
    yyyyyy 2334 3000
    uuuuuuic 1267 4500 where all the 1st col is names,2nd col empno,3rd col salary .

  4. #4
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    this is the code I wrote:

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    main()
    {
    FILE *inFile,*outFile;
    char buffer[2000];
    inFile=fopen("emp.txt","r");
    	if(inFile==NULL)
    	{
    	printf("No such file !");
    	exit(0);
    	}
    
    outFile=fopen("emp_out.txt","w");
    	if(outFile==NULL)
    	{
    	printf("No such file ");
    	exit(0);
    	}
    
    while( fscanf(inFile,"%s",&buffer) > 0)
    	system("clear");
    	printf("%s",buffer);	
    
    fclose(inFile);
    fclose(outFile);
    }
    Last edited by yashpal; 06-04-2006 at 11:19 PM.

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *in;
        char buf[BUFSIZ];
        char tmp[BUFSIZ];
        int i;
        
        if ( (in = fopen( "emp.txt", "r")) == NULL )
        {
             printf("Could not open file.");
             exit(1);
        }
        
        for ( i = 0; (fgets( buf, BUFSIZ, in )) != NULL; i++ )
        {
              sscanf( buf, "%s", &tmp);
              printf("%s\n", tmp);
        }
        
        system("PAUSE");	
        return 0;
    }

    Though, this method assumes there will be no spaces in the employee name fields.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    That was a great help to me bivhitscar.Ironically ,though it din't help me get the names it helped me get the empno only, which in a way, I was trying to retrive in another file . But , it would have been a great help if I could see the list of names on my terminal .
    Thanks a lot .
    yashpal .

  7. #7
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    I have a few doubts though.
    1.
    Code:
     sscanf ( buf, "%s", &tmp) ;
    . would sscanf () read the strings (and only strings ) from the file irrespective of the column of the string ?
    2.
    Code:
    system("PAUSE") ;
    returns sh:PAUSE:command not found .I am programming in Linux OS.
    what could be the alternative for PAUSE.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1. Yes. sscanf() works just like scanf() except the source of the input is not your keyboard, it's a string. So the way bvhitscar is using it, it reads from the buffer that fgets filled. And fgets reads until that buffer is filled or the end of file occurs.

    2. This is reason #3 that using system is a bad idea. A program only pauses when waiting for input, so use something like getchar() instead.

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Oh, you're on linux - that may be the reason it's not working. On windows with dev-c++ it grabbed the customer name great. And the alternative to system("PAUSE") can be seen here - http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    sscanf works exactly like scanf except that it reads from a specified string. Using the %s modifier with it will read input until it reaches a space, newline or eof. So, it will usually only read the first column.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    The code you gave works perfectly.Can you help me customize the program to return names irrespective of the column, for a simple reason that , some files do not have names in the first col .
    Thanks !
    All Learning begins with one phrase " I don't know "

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    After you read the name, use fgets() to read the rest of the line.

    Since buffer is a char*, you don't need the & on the fscanf()

    Do you really want to clear the screen each and every time you read a name? Then when the loop is done, write only the last name read? Always use braces around IF, FOR, WHILE code blocks.

    And when you fix the braces, why do you want to strobe the names on the top of the screen every millisecond or so? The system("clear") is not helpful to you.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    That makes life a fair bit harder. I can tell you that no matter what, you always need to know which column the names are in. Either that, or incorporate some pretty hefty data checking.

    One method could be to use strtok() within the for loop, using the space character as the token. Though again, this can get quite tricky if there is a chance of having spaces in the names.

    Each set of data will probably need it's own processing algorithm. Well, I'm sure if you spent enough time on it, you could write one that could take different types of input - but that could be extrememly time consuming.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  13. #13
    Registered User
    Join Date
    Jun 2006
    Location
    Hyderabad,India
    Posts
    9
    Here's an intersting link . http://www.phy225.dept.shef.ac.uk/me...oblems_Class_1
    Plz check the solution of the problem. The code compiles without any errors, but it does not return the expected results. Where is the bug ?

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    I would search for the first space in the file, line by line, then scan therest of the line to see ifthere are 2 spaces total, if not, then take the next space as the 'first' space in teh file and contiue on...

  15. #15
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I really hope I'm not doing your homework for you.

    (changes in red)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DELIM " "
    
    int main(int argc, char *argv[])
    {
        FILE *in, *out;
        char buf[BUFSIZ];
        char tmp[100];
        char * place;
        int column = 2;    //this is the number of the column where employee name is stored, 2 was just my test value
        int i, j;
        
        if ( (in = fopen( "emp.txt", "r")) == NULL )
        {
             printf("Could not open file.");
             exit(1);
        }
        
        for ( i = 0; (fgets( buf, BUFSIZ, in )) != NULL; i++ )
        {
              place = strtok( buf, DELIM );
              
              for ( j = 1; j < column; j++ )
              {
                  place = strtok( NULL, DELIM );
              }
              
              sscanf( place, "%s", tmp);
              printf("%s\n", tmp);
        }
        
        system("PAUSE");	
        return 0;
    }
    Again, ignore or change the system pause for your system. Yes, I know I should change, I'll do it soon. And this code probably isn't rock solid, it's just the bare essentials to get the job done.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. How to read Records in X-Base files thru C/c++
    By hskambo in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 02:48 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM