fscanf search file HELP!

This is a discussion on fscanf search file HELP! within the C Programming forums, part of the General Programming Boards category; Yeah im pretty new to programming, but I need help understanding fscanf my code is supposed to scan a file ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    fscanf search file HELP!

    Yeah im pretty new to programming, but I need help understanding fscanf

    my code is supposed to scan a file for "abc" and the print the next word that comes after, then keep going in search of another "abc" and do the same

    here is the file to search
    Code:
    abc Purdue sanctions abc professor for research misconduct, abc
    Abc Purdue's undergraduate engineering abc, business ranked among tops in nation abc
    abc Aug. 28 - Black Cultural Center abc abc abc announces fall 2008 Cultural Arts Series
    Aug. 28 - Activities Bonanza to feature variety of student organizations abc abc abc
    then here is my attempt at writing code, its definitely unfinished
    Code:
    #include<stdio.h>
    
    #define ABC 4
    
    int main()
    {
      FILE *finput;
    
      finput = fopen("input","r");
    
      search(finput);
    
      fclose(finput);
    
      return(0);
    }
    
    void search(finput )
    {
      int i,abc[ABC]={"a","b","c",0},test[ABC];
    
      for(i=0,)
      {
        fscanf(finput,"abc",test[ABC]);         
        if(strcmp(test,abc)==0)
        {
          fprintf("%d",&abc);
          fscanf(finput,32);
        } 
      }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can not initialize an integer array with three strings and a 0.

    You should re-read about fscanf to see how input is done.

    You should re-read about fprintf to see how output is done.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Don't forget your function prototypes or types

    Code:
    void search(FILE *finput)
    Also, don't forget to do some kind of error checking when you open the file.
    Code:
      finput = fopen("input","r");
      if (finput == NULL)
        errorhandling();

    Are you trying to declare everything here as an integer?

    Code:
      int i,abc[ABC]={"a","b","c",0},test[ABC];
    for(i=0,)? Try using fgets in a while loop to input data line by line from the file. Also, strcmp() will compare entire strings. Use strncmp to compare only a specific number of characters.
    Code:
    FILE *pfile = fopen("somefile.txt", "r");
    if (pfile == NULL)
        return FALSE;
    
    while (fgets(buffer, BUFFER_LENGTH, pfile) != NULL) {
        strncmp(buffer, "abc", 3);
        //dostuff
    }
    Don't forget to append a line feed (\n) to the end of printed data. I don't know the correct terminology for this, but text won't appear on the console until you do.

    Code:
          fprintf("%d\n",&abc);
    Good luck!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    awesome thanks

    what i was trying to do in the declare step, was to make abc[ABC] equal "abc" and i put the /o thing on the end, or i was trying to, and i guess thats not how it works...

    im still unsure how fscanf works, i re-read it and everything, but its still confusing to me

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Other things I forgot to mention: the whole point of #define is to have a meaningful name for a constant -- ABC is not a meaningful name for the constant 4.

    If you know scanf, then you know fscanf. You may find it easier, as mentioned above, to read in line at a time with fgets and then search inside that string. If you're allowed to use them, I would look at the string handling functions in <string.h> if I were you.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    thanks a lot, both of you, i got it!! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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