pull out data frm a text file based on a condition

This is a discussion on pull out data frm a text file based on a condition within the C Programming forums, part of the General Programming Boards category; My txt file is of the format: Date Station Operator Task Action and a list of values under each heading.. ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    pull out data frm a text file based on a condition

    My txt file is of the format:

    Date Station Operator Task Action

    and a list of values under each heading.. Now how do i write a C code to pull up records tht match a specific value under "TASK"...... say for example, if Task = Vendor Setup, then the result shld show me all the records whr task was equal to vendor setup...

    Thanx in advance !!!!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    I'd probably scan all the files into an array and then when the specific column is called you can print out the data in that column.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    i dint quite get tht..cud u help me with the codin part of it.. i need to first open the file using fopen and then perform the action...

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Show us your try. We won't do your homework for you.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    This should get you started.
    Code:
    #define COLS 5 //for the amount of titles you have
    #define ROW ?? //need to put how many of each rows you have
    
    int main(void)
    {
       FILE *fpIn;
       int arr[ROW][COLS];
       int i, j;
       
    
       if ((fpIn = fopen("filename.txt", "r")) != NULL)
       {
          {
             for(i = 0; i < ROW; i++)
              {
                for(j = 0; j < COLS; j++)       
                fscanf("fpIn", %d, arr[i][j]);
               }
       
          }
          fclose(fpIn);
       }
    
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    The code so far i've written is:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
       FILE *in_file;
       char Operator[5];
       char Task[20];
    
       in_file = fopen (data.txt, "r");
       
       if (in_file == NULL)
       {
          printf ("Error opening file \n");
          exit (-1);
       }
    Now am stuck as to how I need to read the task column frm text file 2 array and chk for the condition...

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    I wudnt be able to quote the no. of rows and columns as this text file is auto copied frm the log file on the database !!!!

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I wudnt be able to quote the no. of rows and columns as this text file is auto copied frm the log file on the database !!!!
    True, but you could find those out for a given file.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    The problem is the txt file gets updated with the log file... so its not possible to count the no. of rows and columns every time !!!!
    I wrote a piece of code...
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
     
    int main()
    {
      FILE *in_file;
      char dataFile[20];
      char date[20];
      char operator[30];
      char ts[80];
     
      printf("&#203;nter file name");
      scanf ("%s", dataFile);
     
      in_file = fopen(dataFile, "r");
     
      
      fgets(ts,sizeof(ts), in_file);
     
     
      if ( strstr(ts,"Vendor Setup" != NULL))
      
       {
          printf("Data found");
       }
      
       return 0;
    }
    On compiling, it gave me a warning msg..but it did create the .obj and .exe files.. When i tried to run it, it prompted me to &#203;nter the filename... when i entered the name, it went blank and gave an error msg tht it has 2 close !!!!

    Cud u plz guide me thro' whr i've gone wrong ????

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    if ( strstr(ts,"Vendor Setup" != NULL))
    you shouldn't ignore the warnings

    if ( strstr(ts,"Vendor Setup") != NULL)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Vart,

    I did correct it but stil is the same..the warning msg comes no more but then when i enter the name of file, it closes off completely with an error !!! Cud u plz guide me thro'whr am doin a mistake !!!

    Sure, wud make note of warning msgs hereon, i actually thght they were not 2 be bothered abt til u let me knw !!! Thnx once again !!!

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    what is the warning?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    thrz no warning msg any more.... it jus shows " filterdaa.c has encountered an error and needs to close" this comes up when i run the program and when it prompts me 2 enter the file name....

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Try:
    Code:
    char dataFile[20];
    
    if ( fgets( dataFile, sizeof(dataFile), stdin) != NULL ) {
       char *newline = strchr( dataFile, '\n');
       if ( newline != NULL )
          *newline = '\0';
    }
    or some variant. fgets will shove the \n character in a buffer if there is room for it. Otherwise it is left in the stream. This newline character can mess up comparisons and filepath validations, as in this case.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    1. it works fine for me
    2. probably - you enter the wrong file name, so your in_file pointer is null but you didn't check it
    3. add checks for return values of the fopen and fgets
    4. check for the buffer overrun in scanf (or replace it with fgets or at least change the format to %19s
    5. 20 characters is not enough for the general file name
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

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