#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12

    c,read text file only one row.


    how can i use c read a text file with one row and one row?
    thinks!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char line[133];
    	FILE *fp;
    
    	if(argc == 1)
    	{
    		printf("-no input file name is given");
    		exit(0);
    	}
    
    	if((fp=fopen(argv[1], "rt")) == NULL)
    	{
    		printf("cannot open file %s", argv[1]);
    		exit(1);
    	}
    
    	while((fgets(line, 132, fp) != NULL))
    		printf("%s", line);
    
    	fclose(fp);
    
    	return 0;
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12
    thanks,but,my program doesn't work,it is like this: i just want to compare two files by row by row.
    main code:

    -------------------------------
    fgets(line1,132,fp1);
    while((fgets(line2,132,fp2)!=NULL)){
    i++;
    if (line1==line2)
    {
    printf( "%s",line1,"is:",i);
    }
    }
    --------------------------------

    and, if the second file have one same row in first file, write a "*" into the same row (begining) in the first file .just this,but it doesn't work.i want to learn it,but now i know c very little,so.....
    can you tell me some,thanks!!really!!!big thank!
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    You cannot compare strings like that in C.
    You need to use strcmp or strncmp, etc...

    i.e.
    Code:
    if (!strcmp(line1, line2)) {
        //they match
    } else {
        //they don't
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Hi,

    Why can't you compare the line1==line2 way? Because line1 and line2 are in fact pointers, adress numbers and they wont ever be the same unless they point the same thing.
    strcmp() simply checks line1[i]==line2[i] for all possible i and if all them match you get a zero, that's why there is a !strcmp().
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Furthermore, the reason why strcmp returns a zero for "true" is that strcmp can return three possible results:

    for strcmp(line1,line2) :
    0 -- they are equal
    <0 -- line1 is less than line 2
    >0 -- line1 is greater than line 2

    Of course, most of the time we are only interested in whether they match or not, but the less-than/greater-than test can be handy at times.

    Also keep in mind that the strcmp comparison is case-sensitive, so that "ABCD" will be less than "abcd", not equal. If you need to make a case-insensitive test, then use stricmp.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12
    thanks everyone,you let me have known more about these,and,i have tried,but i can writ a sign-- "*" at the same row'beginning in the first file.

    the main code:

    --------------
    FILE *fp1,*fp2;
    char line1[255];
    char line2[255];
    int i=0,j=0,k=0;
    while ((fgets(line1,255,fp1)!=NULL)){
    j++;
    while((fgets(line2,255,fp2)!=NULL)){
    i++;
    if (!strcmp(line1,line2)) {
    k++;
    printf( "%d,%d,%d->: %s",j,i,k,line2);
    fputc('*',fp1);
    }
    }
    rewind(fp2);
    i=0;
    }

    --------------

    something wrong?
    sorry,alway ask you these sample things.
    thank you,


    `
    Last edited by kody; July 31st, 2003 at 02:25 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Do you get any runtime error? Or it simply does not do that? Could you show us the output?
    And please, get used to indent!! It's a very good practice.
  16. #9
  17. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    actually, just put your code inside [ code] [/ code] tags.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12
    o,ok,
    output only one row:
    2,2,1->: 4567456
    ----------
    but if cut off this line: "fputc('*',fp1);"
    the output will be :
    2,2,1->: 4567456
    3,3,2->: lkhjlhjklhjklhjklsrq fsdrf
    4,6,3->: ghkjghj
    5,7,4->: dfgsdfgsdfgdfgdsfgdfg
    ----------
    anything wrong?
    but i want to write something into the same row'biginning in the first file. if i cut off "fputc('*',fp1);" it can compare the whole file but can't write anything,why when i add "fputc('*',fp1);" it doesn't work?
    who can help me?thanks!!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12
    and,the two files:
    1:
    ------------
    fghfg
    4567456
    lkhjlhjklhjklhjklsrq fsdrf
    ghkjghj
    dfgsdfgsdfgdfgdsfgdfg
    ------------
    2:
    ------------
    afghfg
    4567456
    lkhjlhjklhjklhjklsrq fsdrf
    fgdfgdfg
    6
    ghkjghj
    dfgsdfgsdfgdfgdsfgdfg
    asdvmalsdvm
    alsdmvlas
    --------------
    and,if i don't cut off "fputc('*',fp1);" ,it can write just one "*" into the first row in the first file,but just once.then stop.....
    why?
    thanks.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Location
    BCN
    Posts
    84
    Rep Power
    13
    Hi,

    I assume that you want to (prepend) append the "*" to the matching line in the file, don't you?
    In that case that's a bit more complex, you have to open the file in update mode "+" option for fopen (not "w", because it truncates the file to 0 lenght, that's why you get only the *. Why does it stop after the first line? No idea, maybe there is some error, check the return value of fputc, is it eof?) Once the file is opened this way you'll have to find the position in the file where you want to write to, with fseek and so on (look stdio.h reference).
    A solution would be to open a new file ("w" option) and write the line if it does not match and write "*"+line if it does match. Remember to include a "\n" character in the end of the lines so it jumps lines.
    Hope this helps.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    31
    Rep Power
    12
    i did,but nothing change.why?
    can you tell me how to do?
    thanks
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    3
    Rep Power
    0
    Hi!
    am not sure..but should'nt you open the files first?

IMN logo majestic logo threadwatch logo seochat tools logo