Thread: File Names

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    File Names

    hi all
    this is my code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define BUF 250
    main()
    {
    FILE *fp;
    int i;
    char buffer[BUF],*guffer;
    fopen("log.txt","r");
    while((fgets(buffer,BUF,fp))!=NULL)
    {
    for(i=0;(i<streln(buffer))&&((*(buffer+1))!='\0');i++)
    {
    switch(*(buffer+i))
    {
    case ' ':
     guffer=strudp(buffer+i);
    }
    }
    }
    printf("&#37;s\n",guffer);
    fclose(fp);
    }
    what it does is next
    there is a file called log.txt
    it's content is
    c:\mybayby\storm\links.php

    c:\mybayby\storm\yah.php

    c:\mybayby\storm\soso.php

    all what i want is to get the file names like links.php
    then take the path and copy it
    how can i do this
    ?
    this one works but i can't modify it to work like i told you
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define BUF 250
    main()
    {
        char fi[80],gg[80];
        gets(fi);
        strcpy(gg,fi);
         int i;
         FILE *copy,*cc;
        char *name;
        char buffer[BUF];
         for(i=0;(i<strlen(fi))&&((*(fi+i))!='\0');i++)
         {
         switch(*(fi+i))
         {
         case '\\':
              name=strdup(fi+i+1);
              }
              }
              printf("%s",fi);
              cc=fopen(gg,"r");
              copy=fopen(name,"w");
              while((fgets(buffer,BUF,cc))!=NULL)
              {
              fputs(buffer,copy);
              }
             fclose(copy);
             fclose(cc);
              }
              /*end of get file names copy*/
              /*EOF*/
    help

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    all what i want is to get the file names like links.php
    then take the path and copy it
    Can you describe that a different way? You're getting the file names, but what for? Are you throwing them away or using them? What do you mean by copy the path?

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    thanks
    all what is need is
    c:\mybaby\storm\links.php
    i want to get the file name
    links.php
    then
    copy it to another location
    thy are 4 files for example
    there path's in the file storm.txt
    now the program gets me the file names and paths and copy them
    just it

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Now you're saying something different and still not being clear. Here's one line:
    Code:
    c:\mybaby\storm\links.php
    This is the path:
    Code:
    c:\mybaby\storm\
    And this is the file:
    Code:
    links.php
    Which one do you want to copy and where do you want to copy it to?

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    will sorry for not being clear but i am not so power full in en
    any whay
    i want to copy the file
    but it's not just one file they are about 20 files for example
    just it

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do 20 files in a loop just like the first one.

    So you should explain what you want to do with the first one, show the code that does it, and describe the problem, where do you fail.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by vart View Post
    you do 20 files in a loop just like the first one.

    So you should explain what you want to do with the first one, show the code that does it, and describe the problem, where do you fail.
    i already posted my code
    for example
    you have file like this ok
    /puplic_http/home c:\mybayby\storm\links.php
    sos sdkhfafkshfkajhfkafhkasfhaklsf
    /puplic_http/home c:\mybayby\storm\yah.php
    aksfhksfhasklfhlaksfhaskfhalksfh
    gogogogogogo

    asgsagsa
    agagasg
    /puplic_http/home c:\mybayby\storm\soso.php
    hahaha

    i have coded a code to extract the paths
    c:\mybayby\storm\links.php
    c:\mybayby\storm\yah.php
    and put them in a file called log.txt this one is ok
    then from this file i want to copy all files under the paths stored in it
    GOT it

  8. #8
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    looooooooooooooooooooooooooooooooooooooooooooooooo ol
    i have done it
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define BUF 250
    main()
    {
    char buffer[BUF];
    char *ex;
    int i;
    FILE *fp;
    printf("ENter A file Path:");
    fp=fopen("log.txt","r");
    while((fgets(buffer,BUF,fp))!=NULL)
    {
    for(i=0;(i<strlen(buffer))&&((*(buffer+i)) != '\0');i++)
    {
    switch(*(buffer+i))
    {
    case '\\':
         ex=strdup(buffer+i+1);
    }
    }
    printf("\nbuffer=&#37;s\nfilename=%s",buffer,ex);
    }
    }
    thanks all

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some indentation skills would come in handy - like making it more likely for people to help you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    thanks salem
    now i have much bigger problem
    this
    i have this in my log
    Code:
    Apr 22 04:20:08 host pure-ftpd: ([email protected]) [NOTICE] /home/storm//filename.php uploaded  (1423 bytes, 3.03KB/sec)
    now all what i need is to get the file name
    and copy it
    how can i do this?
    all what i need is to save the file path in a text file called log.txt
    and copy the file to the same directory the program lunched from ?
    thanks again
    Last edited by St0rM-MaN; 04-30-2007 at 08:43 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read the line using fgets

    Then either
    - count a number of spaces (7)
    - use strchr() to locate the '/', if it is reliably the first field containing a /
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by Salem View Post
    Read the line using fgets

    Then either
    - count a number of spaces (7)
    - use strchr() to locate the '/', if it is reliably the first field containing a /
    Read the line using fgets
    thats ok

    - count a number of spaces (7)
    you mean by
    strchr(buffer,' ');

    - use strchr() to locate the '/', if it is reliably the first field containing a /
    i did not get that
    can you explain more?
    you mean locate the first occurrence of / till the next white space?
    like this for example
    Code:
    char *name=malloc(100*sizeof(char));
    fgets(buffer,BUF,fp);
    if(name=strchr(buffer,'/') == 0)
    {
    for(x=0;x<strlen(name);x++)
    {
    switch (*(name+x))
    {
    case '  ':
    name[x]='\0';
    }
    }
    }
    sorry i have no machine now to tray can you tell me is that ok?
    thanks again
    Last edited by St0rM-MaN; 05-01-2007 at 03:18 AM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, something like that.

    Except you don't need to malloc a pointer - strchr() returns a pointer to somewhere inside an existing string (or NULL if the character is not found).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    huh i am so tired at this moment
    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    #include<string.h>
    
    #define BUF 250
    
    main()
    
    {
    
    char buffer[BUF],word[BUF],*name;/* buffer for getting line , search word , where will the path stored*/
    
    FILE *fp,*cp,*cc;
    
    int x,n;
    name=malloc(100 * sizeof(char));/* allocating enough space for handling the file path*/
    fp=fopen("storm.txt","rb");/*open the file witch contine the file pathes */
    cp=fopen("log.txt","wb");/*open the log.txt where the files paths will be stored*/
    scanf("&#37;s",word);
    while((fgets(buffer,BUF,fp))!=NULL)
    {
    
    if((strstr(buffer,word)!=NULL)&& (strstr(buffer,"uploaded")!=NULL))
    {
    for(x=0;(x<strlen(buffer))&&(buffer[x]!='\0');x++)/* for loop for counting the charctes one by one and compare them with / */
    
    {
    
    if(buffer[x]=='/')  /* here we say if the buffer equals / do this like the first occurrence*/
    
    {
    name=&buffer[x];/* assgining the adress of the / to the first adress for name */
    for(n=0;n<strlen(name)&&name[n]!='\0';n++)/* for loop for  counting chracters one by one to compare the with white space*/
    {
    switch((*(name+n)))/*switching the content's of name*/
    {
    case ' ':/*compare it with white space*/
    name[n]='\0';/*if it is a white space end the string */
    }/*end of switch*/
    }/* end of the secode for loop*/
    fputs(name,cp);/* now write the path of the file */
    fputs("\n",cp);/* ading a new line fputs does not do this*/
    break; /* end the if statment because it will never stop till the end of the string and that will make it does this {if it see ant chracter / it will do it's stuff tray and remove it it will make it like this
    /blah/blah/blah
    /blah/blah
    /blah
    GOT it?
    */
    }/*end of the third  if (buffer[x]=='/') */
    }/* end of the first for loop*/
    }/*end of the first if*/
    }/* end of while*/
    fclose(fp);
    fclose(cp);
    }
    works perfectly
    but when it comes to the real deal
    it is not working
    the file is a 24.0 MB and it's saved as a unicode 8utf
    so what is the problem?
    thanks so much;

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Please post indented code.

    > n<strlen(name)&&name[n]!='\0'
    This amounts to the same thing, if one is true, then so is the other.
    Just do
    name[n]!='\0'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM