Like Tree1Likes
  • 1 Post By whiteflags

c code requested- really hard problem

This is a discussion on c code requested- really hard problem within the C Programming forums, part of the General Programming Boards category; i am writing a code which is as follows: # Code: include <stdio.h> int main(void) { char buffer[2500]; FILE * ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    15

    c code requested- really hard problem

    i am writing a code which is as follows:

    #
    Code:
    include <stdio.h>
    
     int main(void)
     {
     char buffer[2500];
     FILE * myfile;
    
     myfile = fopen("/home/csgrads/akhan015/desktop/benchmark/coverage/test0/stmt.txt","r");
    
     while (!feof(myfile))
     {
     fgets(buffer,2500,myfile);
     printf("%s",buffer);
     }
    
     fclose(myfile);
    
     return 0;
     }
    1.in this code i am accessing a folder to read a text file. this test0 folder is only 1 folder of the 1500 folders i have to read in which i have to open and compare 1500 txt files which are in test0... test1500. i can only access the text file through path( this is the only way i know to access the text file placed in a folder).please can someone tell how can i access 1500 folders in a loop or some other way. a code snippet would be really appreciated.

    2.for each of 1500 text files and i have to compare the strings in them such that if two files have similar strings then i keep only 1 of it in a buffer and compare it with the next file. if theses two have some strings different then i will unite them.and remember the folder name e.g test 19, test 100 in a separate file.if somebody has an idea abt it please help me.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What operating system is this for?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well, maybe this will help to a great degree: Cprogramming.com FAQ > Accessing a directory and all the files within it

    I think you're asking a little much from the one or two people that will answer this, but all operating systems have their own API you can use for traversing directories (and since you chose C, this is as good as it gets). Get familiar with the way your OS does it. Then you know that part.

    As a tip, it's an exceedingly good idea to work with one or two files open at a time instead of 1500. So devise a step by step algorithm that will do what you want and then write it in C.
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    15
    "What operating system is this for? "

    i am sorry i forgot to mention: linux

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Save your path as a string: pathFile. Include string.h header file, and strcat "/test" onto your pathFile array (or just assign the letters if you prefer). Then keep a number for the file you want to open next (and which you will increment as each file is handled, in your big for() loop for this).

    To get the right file name, increment your file number, and itoa() it over to a small char array - remember to keep an extra element for the end of string char.

    Now append that only the pathFile string with strcat(), and you have your next file to be opened.

    And if your file number starts with 0, and you have 1500 files, your highest file number will be 1499 (of course).

    This is all straight forward string work, so don't let the 1500 files number scare you - if you can do this for 5 files, you can do it for 1500 files.

    The tricky part in your description was the "similar" strings part. You'll have to really nail that down for the computer, in your program. But don't worry about the file handling. It's no problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another hard C problem
    By alexbcg in forum C Programming
    Replies: 12
    Last Post: 11-23-2010, 07:03 AM
  2. Having a hard time interpreting this code
    By BLG in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 05:19 PM
  3. Code Check Requested! (Alphabetize entries?)
    By theonetruehero in forum C Programming
    Replies: 6
    Last Post: 04-15-2008, 04:43 PM
  4. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 12:44 PM
  5. Hard Problem :(
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2002, 03:16 AM

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