problem with reading from file

Discussion in 'C Programming' started by mitek777@o2.pl, Apr 13, 2006.

  1. Guest

    Hi, I have a problem with reading from file. I would like to find some
    string(f e.g. name) in file, and if it exist show it on screen with
    second and third line under.

    I have this in file:

    name
    surname
    age


    #include <stdio.h>
    #include <conio.h>

    FILE *file;

    main(){

    char s[30];
    int line = 0;
    char name[]="kate";
    file=fopen("text.txt","r");

    if(!file) {
    printf("file doesnt exist");
    getch();
    return 0;
    }

    while( line<4 )
    {

    fgets(s, sizeof(s), file);
    if (strcmp(s,name)==0) printf("%s",s);
    else {
    printf("didnt find a string");
    getch();
    return 0;
    }
    line++;

    }
    fclose (file);
    getch();
    }
    , Apr 13, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi, I have a problem with reading from file. I would like to find some
    > string(f e.g. name) in file, and if it exist show it on screen with
    > second and third line under.
    >
    > I have this in file:
    >
    > name
    > surname
    > age
    >
    >
    > #include <stdio.h>
    > #include <conio.h>
    >
    > FILE *file;
    >
    > main(){
    >


    I'll point out here that you need to have int main() and you need to
    return something.
    What exactly is your problem with this code?
    Andrew Poelstra, Apr 13, 2006
    #2
    1. Advertising

  3. Andrew Poelstra wrote:
    > wrote:
    > > Hi, I have a problem with reading from file. I would like to find some
    > > string(f e.g. name) in file, and if it exist show it on screen with
    > > second and third line under.
    > >
    > > I have this in file:
    > >
    > > name
    > > surname
    > > age
    > >
    > >
    > > #include <stdio.h>
    > > #include <conio.h>
    > >
    > > FILE *file;
    > >
    > > main(){
    > >

    >
    > I'll point out here that you need to have int main() and you need to
    > return something.
    > What exactly is your problem with this code?


    Sorry; you did return a value from main.
    Note: When I attempted to compile this, I found that I did not have
    conio.h. What is this header supposed to do?
    Andrew Poelstra, Apr 13, 2006
    #3
  4. Andrew Poelstra wrote:
    > Andrew Poelstra wrote:
    > > wrote:
    > > > Hi, I have a problem with reading from file. I would like to find some
    > > > string(f e.g. name) in file, and if it exist show it on screen with
    > > > second and third line under.
    > > >
    > > > I have this in file:
    > > >
    > > > name
    > > > surname
    > > > age
    > > >
    > > >
    > > > #include <stdio.h>
    > > > #include <conio.h>
    > > >
    > > > FILE *file;
    > > >
    > > > main(){
    > > >

    > >
    > > I'll point out here that you need to have int main() and you need to
    > > return something.
    > > What exactly is your problem with this code?

    >
    > Sorry; you did return a value from main.
    > Note: When I attempted to compile this, I found that I did not have
    > conio.h. What is this header supposed to do?


    That header provides DOS-specific console I/O, and is found in Borland
    C products.

    For some reason, lots of newbies like to use `getch()` it provides,
    instead of the standard `getchar()`. This is usually to prevent the
    console window closing after the program finishes in IDEs that won't
    keep them open for you.
    Vladimir S. Oka, Apr 13, 2006
    #4
  5. ais523 Guest

    Vladimir S. Oka wrote:

    > Andrew Poelstra wrote:


    <snip earlier discussion>

    > > Note: When I attempted to compile this, I found that I did not have
    > > conio.h. What is this header supposed to do?

    >
    > That header provides DOS-specific console I/O, and is found in Borland
    > C products.
    >
    > For some reason, lots of newbies like to use `getch()` it provides,
    > instead of the standard `getchar()`. This is usually to prevent the
    > console window closing after the program finishes in IDEs that won't
    > keep them open for you.


    I have had legitimate cause to use <conio.h> in the past; it accesses
    various DOS console I/O functions which cannot be written portably
    (e.g. there is no standard C way of writing text in a particular
    colour). The (minor) advantage of getch() is that it circumvents
    buffering (i.e. you don't have to type a newline at the end of input
    like you do with most getchar() implementations). However, I would
    recommend to those newbies that they should steer clear of <conio.h>
    unless they actually need it; like curses, it is incompatible with some
    standard output functions, including printf. Basically, you have to
    decide whether a program is a 'conio program' or not before writing it,
    and use an appropriate set of functions. As the people on comp.lang.c
    use many different systems, and even the newbies Vladimir mentions may
    upgrade their system or change to a different operating system some
    day, it really isn't worth using conio.h unless you have to. (Despite
    the message it replies to, the above diatribe is directed partly at the
    OP but mostly at hordes of imaginary newbies that like to use conio.h.
    These are possibly the same newbies who think starting a program with
    clrscr() is a great idea.)
    ais523, Apr 13, 2006
    #5
  6. Simon Biber Guest

    wrote:
    > Hi, I have a problem with reading from file. I would like to find some
    > string(f e.g. name) in file, and if it exist show it on screen with
    > second and third line under.


    What is "(f e.g. name)"? Just "(e.g. name)" would do. What is that "f"
    doing there?

    Simon.
    Simon Biber, Apr 13, 2006
    #6
  7. MH Guest

    what can i use instead of getch() ? is something wrong with using that
    function? maybe use this:
    void stop(){
    char c;
    while ((c=getchar())!='\n'){}
    }

    The problem is: I want to find some word in my file and when I find it
    show it on screen with second and third line under.
    for example: I want to find name: "kate" //char name[]="kate"; when
    the program it, show the name, and read form file two lines under
    MH, Apr 13, 2006
    #7
  8. "MH" <> wrote in message
    news:...
    > what can i use instead of getch() ? is something wrong with using that
    > function? maybe use this:
    > void stop(){
    > char c;
    > while ((c=getchar())!='\n'){}
    > }


    Include context in your replies. The problem with getch() is that it is
    non-standard which constrains the portability of your programs. You can use
    the standard getchar() which has similar functionality.


    > The problem is: I want to find some word in my file and when I find it
    > show it on screen with second and third line under.
    > for example: I want to find name: "kate" //char name[]="kate"; when
    > the program it, show the name, and read form file two lines under


    When you find the word you are looking for, read two more lines from your
    input file. You could use fgets() to do this, or even fgetc() to read
    character by character displaying each character at the same time until you
    have read two newline characters.
    stathis gotsis, Apr 13, 2006
    #8
  9. Pedro Graca Guest

    wrote:
    > Hi, I have a problem with reading from file. I would like to find some
    > string(f e.g. name) in file, and if it exist show it on screen with
    > second and third line under.


    [snip file contents]

    > #include <stdio.h>
    > #include <conio.h>
    >
    > FILE *file;
    >
    > main(){
    >
    > char s[30];
    > int line = 0;
    > char name[]="kate";
    > file=fopen("text.txt","r");
    >
    > if(!file) {
    > printf("file doesnt exist");
    > getch();
    > return 0;
    > }
    >
    > while( line<4 )
    > {
    >
    > fgets(s, sizeof(s), file);
    > if (strcmp(s,name)==0) printf("%s",s);


    Here you are comparing what you read from the file with the contents of
    the name array.
    The array has "kate";
    whatever you read from file will have a trailing '\n' and cannot ever be
    equal to "kate".

    So, before comparing the strings, remove the terminating newline from s.

    [rest of code snipped]

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Apr 13, 2006
    #9
  10. stathis gotsis wrote:
    > "MH" <> wrote in message
    > When you find the word you are looking for, read two more lines from your
    > input file. You could use fgets() to do this, or even fgetc() to read
    > character by character displaying each character at the same time until you
    > have read two newline characters.


    I find that fgetc is much easier to use, because checking for
    whitespace and comments doesn't require any pointer or array
    manipulation.
    Andrew Poelstra, Apr 13, 2006
    #10
  11. Guest

    > Sorry; you did return a value from main.
    main()
    { }

    it is urgly, but legal.
    , Apr 14, 2006
    #11
  12. Pedro Graca Guest

    mitek mailed my dodgeit address and I happenned to look at it
    <http://www.dodgeit.com/run/checkmail?mailbox=hexkid>

    Please don't followup on usenet articles by email.
    Many people use an invalid address and your question will never be seen
    by them; also questions and answers on usenet will be reviewed by
    several people, and mistakes are much more likely to be caught.


    e-mailed:
    > Pedro Graca wrote:
    >> Here you are comparing what you read from the file with the contents
    >> of the name array.
    >> The array has "kate";
    >> whatever you read from file will have a trailing '\n' and cannot ever
    >> be equal to "kate".
    >>
    >> So, before comparing the strings, remove the terminating newline from
    >> s.

    >
    > How can I do that?


    use strlen() function te determine where is the last character of the
    string read from the file

    size_t len = strlen(s);

    if s is "kate\n", len will be 5, and s[4] is the terminating newline

    if ((len > 0) && (s[len-1]) == '\n') {
    /* just delete the newline and update len */
    s[len-1] = 0;
    len -= 1; /* or s[--len] = 0 */
    }

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Apr 14, 2006
    #12
  13. Default User Guest

    wrote:

    > > Sorry; you did return a value from main.

    > main()
    > { }
    >
    > it is urgly, but legal.


    It is syntactically legal, but returns an indeterminant value to the
    host system. That generally isn't a good idea. In C99, it's been
    changed so that falling off the end of main() returns a 0, but what you
    show is not legal C99 code.




    Brian
    Default User, Apr 14, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    657
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,779
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,391
    Scott Allen
    Jun 4, 2005
  4. Replies:
    0
    Views:
    771
  5. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    356
    Hrvoje Niksic
    May 25, 2007
Loading...

Share This Page