read 2nd or 3rd line in txt file?

This is a discussion on read 2nd or 3rd line in txt file? within the C Programming forums, part of the General Programming Boards category; This is one question that I have asked myself a lot but I can never actually answer it. I have ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    read 2nd or 3rd line in txt file?

    This is one question that I have asked myself a lot but I can never actually answer it. I have ran it through my head made some stuff up that might work but has a few problems and some other things that are very "what was i thinking".
    My question: how do I read a certain line in a text file.
    It seems like I am not getting the very obvious solution.

    If you could tell how most people go around doing this by typing some pseudocode or something that would be awesome.
    (I do NOT want your whole program. pseudocode is enough for me).

    (I hope this didn't come off as to pushy)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    One way is to read the first n-1 lines and discard them, then read the nth line.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    right, I actually thought of this the second I pushed the post button.
    Last edited by xniinja; 08-11-2010 at 01:57 PM.

  4. #4
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    I have started on my code, and I am getting some weird results.
    The application doesn't actually go into the while loop. If you could help that would be cool.
    (It might be some simple thing I overlooked).

    Code:
    #include <stdio.h>
    
    main()
    {
          int line = 0;//tell what line it is on
          
          char liner[100];//put the characters of the line into here
          
          int linesearch;//the line you are looking for
          
          FILE *file;//the file pointer
          
          file = fopen("search.txt","r");//point the file
          
          printf("line number:");//print line number...
          
          scanf("%d",&linesearch);//...then scan after the printf
          
          printf("going to line %d",linesearch);//to make it look like programs in TV shows
          
          while(fgets(liner,100,file))//read the file string by string
          {
                                      printf("%d",line);//when a line is read tell what line it is for debug perposes.
                                      
                                      line++;//make 'line' go up once
                                      
          }
          getch();//getch to see what was printed out.
          
    }


    hope it is readable.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You should check the success of your fopen().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    ok I fixed it.
    All of the code...
    bare with my formatting.

    Code:
    #include <stdio.h>
    
    main()
    {
          int line = 1;//tell what line it is on
          
          int found = 0;
          
          char liner[100];//put the characters of the line into here
          
          int linesearch;//the line you are looking for
          
          FILE *file;//the file pointer
          
          file = fopen("C:\\Users\\bob\\Desktop\\hl2\\portal_config.cfg","r");//point the file
          
          
          if (file == NULL)
          {
                   printf("file does not exist or doesn't work\n");
                   return 0;
                   
          }
          
          
          
          printf("line number:");//print line number...
          
          scanf("%d",&linesearch);//...then scan after the printf
          
          printf("\ngoing to line %d\n\n",linesearch);//to make it look like programs in TV shows
          
          while(fgets(liner,100,file))//read the file string by string
          {
                                      if(linesearch == line)
                                      {
                                                    found = 1;
                                                    printf("line %d found,line %d says: %s",linesearch,linesearch,liner);
                                                    
                                      }
                                      
                                      printf("reading line: %d\n",line);//when a line is read tell what line it is for debug perposes.
                                      line++;//make 'line' go up once
                                      
          }
          
          if (found == 0)
          {
                    
                    printf("line number %d was not found",linesearch);
                    
          }
          
          line = line - 1;
          printf("\nthis file has: %d lines",line);
          
          getch();//getch to see what was printed out.
          
    }
    Tell me if you find any more errors

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I'm on a Mac, and if I change the file name, this works for me. What is not working for you?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Tell me if you find any more errors
    What I'm pointing out here is not an error, but rather an issue of style.

    Commenting code is definitely a useful endeavor, and I'm glad to see that you're doing it. However, the art of the comment takes some time to master. Your comments should not simply restate, in English (or whatever language) what the code is doing:
    Code:
    FILE *file;//the file pointer
    printf("line number:");//print line number...
    scanf("%d",&linesearch);//...then scan after the printf
    line++;//make 'line' go up once
    Comments like these do not help the reader understand what is going on. Either he knows C, and the comments are redundant, or he doesn't, and the comments won't help him at all (and comments really shouldn't be aimed at those who don't know the language...)

    Instead, comments are more usefully employed to explain, say, the arguments to a function (can this pointer be NULL? what is the valid range of this data type? and so on); a high-level description of an algorithm; an explanation for something that might appear sketchy or wrong; and so on. Assume the user understands the code itself, but perhaps not why you put it together the way that you did.

    As an example, here is a comment I pulled from a project of mine. This comment describes a table (that is, an array) that maps one value to another:
    Code:
    /* The null character in the alphabet table does not actually
     * signify a null character: character 6 from A2 is special
     * in that it specifies that the next two characters form a
     * 10-bit ZSCII character (3.4).  The code that uses the
     * alphabet table will step around this character when
     * necessary, so it’s safe to use a null character here to
     * mean “nothing”.
     */
    I think this is a useful comment because it explains a quirk in the table (one of the entries isn't really supposed to be used), which hopefully will avoid confusion. In addition, it makes a reference to a specific chapter in a standards document so the reader can verify/look up what, specifically, is being worked around. The name of the document is not mentioned, but for this project that's OK, because it's an implementation of that standard and so is implied.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    In the words of Victor Meldrew - "I don't believe it"

    There was nothing wrong with the commenting in ninja's code - perfectly understandable - in fact a lot easier to get through than the so called style approach. Remember less is more in programming.

  10. #10
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    There was nothing wrong with the commenting in ninja's code - perfectly understandable - in fact a lot easier to get through than the so called style approach. Remember less is more in programming.
    I'm always willing to incorporate new things I learn if I find them to be superior to what I'm doing. So can you articulate what it is about a comment like “make 'line' go up once” that you find useful, when accompanying the code “line++”? I see absolutely no benefit, but if you can tell me what the benefit is, I'm willing to listen. Note that I'm not saying the comments are wrong, or not understandable, but that they're redundant.

    But if less is more, wouldn't that imply no comments at all?

  11. #11
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    Thanks I will try taking your commenting style into effect with my next projects. But when I posted the code I didn't have any errors (with the code) that I was seeing, I just wanted to know if i made a noobish mistake.

    Thanks again.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code with comments for the utterly obvious is annoying. It crowds the page, for nothing.

    What you need to comment on, is what is NOT obvious, when you are reading or working with, the code.

  13. #13
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    OK I will only comment on what isn't so obvious, like why I set the while loop the way i did and stuff.

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When I write comments, I'll write comment blocks on the intention of the block of code. Inputs, outputs, return codes, and a higher level answer to the question "what is this routine's purpose in life?".
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you read a whole line of text from a txt file
    By themexican in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2005, 10:17 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. read a line from file
    By DogsCanDrive in forum C Programming
    Replies: 6
    Last Post: 04-05-2005, 04:41 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM

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