Thread: file concept

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    file concept

    Hi



    any help it should be appreciable.
    Last edited by munna_dude; 06-16-2010 at 12:10 PM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I would approach it like this I think:

    Code:
            char *str = "Dream as if you will hello world forever, hello world as if you will die today";
            char *hello = "ello world";
            int i, j, found = 0;
    
            for(i = 0; i < strlen(str); i++){
                    if(str[i] == 'h'){
                            i++;
                            for(j = 0; j < strlen(hello); j++, i++){
                                    if(str[i] != hello[j]){
                                            found--;
                                            break;
                                    }
                            }
                            found++;
                    }
            }

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    Cprogramming.com - Tutorials - C File I/O would be a good place to start

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok, reading this line "i have read the entire file and search the string." I though you had already read it from a file.

    You need to open the file for reading and read into a buffer. The best is probably to allocate memory for it with malloc, if the file is reasonably sized. You can do that by seeking to the end and use ftell to get the size.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    Quote Originally Posted by LordPc View Post
    Cprogramming.com - Tutorials - C File I/O would be a good place to start
    hmm. i know that..
    but i am unable to read the file,
    my code is only reading last line of the one.txt
    (while (!feof(database) && fgets(line, 256, database) != NULL))
    so, i approached to this forum.

    any help it should be appreciable.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Skip the feof part, you should be able to manage with only testing for NULL from fgets().

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by munna_dude View Post
    hmm. i know that..
    but i am unable to read the file,
    my code is only reading last line of the one.txt
    (while (!feof(database) && fgets(line, 256, database) != NULL))
    so, i approached to this forum.

    any help it should be appreciable.
    Look at your code. It's reading all the lines of the file, into the very same char array, constantly over writing the previous line of text.

    Until only the last line is left intact.

    Preferred is just:
    Code:
    while((fgets(line, 256, database)) != NULL) {
      //rest of your code in here to handle the line array
    
    }
    Note where the (( and )) are placed!
    Last edited by Adak; 06-16-2010 at 01:33 AM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    That is very likely the problem, but it's not been posted in context with the searching. It's possible to do like that if the fgets call is inside the search loop.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What are you trying to do? What is r??

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But you read one line of 256 chars, then you enter an infinite loop with your while(1), so it's not reading more than one time in your case.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    Quote Originally Posted by Adak View Post
    What are you trying to do? What is r??
    "r is an array i need to search for"

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    is anybudy can help with rewrite my program..

    any help it should be appriciable

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If the text appears like it does in your first post it's likely the new line characters that mess this up. Hello world broken up on two lines will give you "h\nello world" for example. You actually only have two occurrences of the string 'r' in the file.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    136
    Quote Originally Posted by Subsonics View Post
    If the text appears like it does in your first post it's likely the new line characters that mess this up. Hello world broken up on two lines will give you "h\nello world" for example. You actually only have two occurrences of the string 'r' in the file.
    yes! the one.txt is as it is.
    can you please run both the programs wich i posted. the first one is with out file concept. it giving the out put has "4".
    but with file concept program is giving the output "2".

    i have to use same text in file concept.( like messup, broken line and all)

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Everything is as it is. You have never posted one.txt, we can only assume here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM