Read from file and make few things

This is a discussion on Read from file and make few things within the C Programming forums, part of the General Programming Boards category; Well I have txt files like IP | separator | name xxx.xxx.xx.xx, name xxx.xxx.xx.xx, name xxx.xxx.xx.xxx, name I wanna read ...

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    Read from file and make few things

    Well I have txt files like

    IP | separator | name
    xxx.xxx.xx.xx, name
    xxx.xxx.xx.xx, name
    xxx.xxx.xx.xxx, name

    I wanna read one line then execute commands from it
    I'll map network drive then execute command's for copying files on that network drive then read next line from that txt and so on.

    Can you help on that.

    Do you have some code that I can look for.

    Or we can code it together.

    Well I have troubles on beggining so I heavent post any code.!

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

    I started coding and I'll ask questions in later down posts. and here I'll edit post of code that I wroted. So you can easly join in coding. Thanks.!
    For now this is done, almost nothing but heh.

    Code:
    // Windows Based Code
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void formatIP(char*buf, char*ip,char*ipname);
    void map_drive(char *ip, char *ipname);
    void copyfile(char *ip, char *ipname, char *drive_path);
    
    int main(void)
    {
        char ch;
        char buf[BUFSIZ] = {'\0'}; 
        char ip[15] = {'\0'}; 
        char ipname[BUFSIZ] = {'\0'}; 
        
        FILE *fpIP;
        fpIP = fopen ("ip_adress_list.txt","rt");
        
        if (fpIP!=NULL)
        {
        // uzmi jedan red iz adress lista
           while (fgets(buf, BUFSIZ, fpIP) != NULL)
           {
                 // formatiraj red iz adress lista ip adresa i ip name
                 formatIP(buf, ip, ipname);
                 map_drive(ip, ipname);
           }                               
        }
        else 
        {
          perror("fopen");
        }
        
        
        system("PAUSE");
        return 0;
    }
    
    void formatIP(char*buf, char*ip,char*ipname)
    {
         int i, j = 0;
         int separator = 0;
         char ch;
              
        for(i=0,j=0;*buf!='\0';buf++)
        {
              ch = *buf;
              if (ch == ',')
              {
                 separator=1;
              }
                 
              if(separator != 1)
              {
                   *(ip+i) = ch;
                   i++;
              }
              else
              {
                  *(ipname+j) = ch;
                  j++;
              }
        } 
        *(ip+i)='\0';
        *(ipname+j)='\0';
    }
    
    void map_drive(char *ip, char *ipname)
    {
         char map_dr[50] = {'\0'};
         const char drive_name[3]="i:\0";
         const char drive_path[3]="c$\0";
         char disc_map_dr[30] = {'\0'};
         char ch;
         FILE *fpMD;
    
         fpMD = fopen("map_drive.bat", "wt");
         
         strcpy(map_dr, "net use ");
         strcat(map_dr, drive_name);
         strcat(map_dr, " \\\\");
         strcat(map_dr, ip);
         strcat(map_dr, "\\");
         strcat(map_dr, drive_path);
         fprintf(fpMD, "%s\nEXIT", map_dr);
         fclose(fpMD);
         system("map_drive.bat");
    
         copyfiles(ip, ipname, drive_path);
         
         //DISCONNECT MAP DRIVE
         strcpy(disc_map_dr, "net use ");
         strcat(disc_map_dr, drive_name);
         strcat(disc_map_dr, " /delete");
         system(disc_map_dr);
         
    }
    
    void copyfile(char *ip, char *ipname, char *drive_path)
    {
         
    }
    Last edited by xxxrugby; 07-14-2006 at 07:19 AM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Read and parse a line with fgets and sscanf.
    FAQ > How do I... (Level 2) > Run a program from within a program
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Maybe you can write a simple parser with lex...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Noticed your edit, a reply may have been more appropriate. This was my play code.
    Code:
    #include <stdio.h>
    
    int main()
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[64], url[16], name[32];
          while ( fgets(line, sizeof line, file) != NULL &&
                  sscanf(line, "%15[^,], %31s", url, name) == 2 )
          {
             printf("url = \"%s\", name = \"%s\"\n", url, name);
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* file.txt
    123.100.10.5, Able
    123.100.10.50, Baker
    123.100.101.5, Charley
    123.10.103.125, Delta
    123.100.10.6, Foxtrot
    */
    
    /* my output
    url = "123.100.10.5", name = "Able"
    url = "123.100.10.50", name = "Baker"
    url = "123.100.101.5", name = "Charley"
    url = "123.10.103.125", name = "Delta"
    url = "123.100.10.6", name = "Foxtrot"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Can I integrate XCOPY with error handling into C code.!
    So If copy fails I can write that into log.
    Something like
    if (filecopy(filename) == ERROR)
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xxxrugby
    Can I integrate XCOPY with error handling into C code.!
    I believe the correct piece of punctuation you were looking for is the question mark. It looks like this: ?

    While I'm not sure if you have the ability to do so, yes, you could in theory replicate XCOPY with error handling.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Create a file in the directory you want to copy to, open up the original, read the original into the new one, delete the original. (Or for a not portable, and sometimes dangerous solution, system calls).

  8. #8
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    It will be non portable. Everything will work on Windows XP Pro.
    Toomorow will came more code from me I hope so.!

    Isnt there some header that have COPY source file to DEST file?
    And that have error handling like TRUE or FALSE.

    And second header that I need is some than will look into dir.
    Then it will read file name. I'll put it into string. Then It'll copy one by one. And give status report like copy ok or copy failed.
    And then I'll put that report into log.!!!!

    Well this code am doing on my own iniciative beacuse am too lazy to do a copy on lots of computers.!

    So some help if someone know for that two headers for direcotry and copy will be appriciated.
    It doesent need to be portable. It's for Windows.!
    Last edited by xxxrugby; 07-13-2006 at 06:58 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  9. #9
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    I will do it with C code. And robocopy.exe from Win 2k3 Server resource kit.!

    How do you wirte char $ inside C code.! I make
    const char map_path[2] = "c$"
    when I wanna write puts(map_path)
    I get outpoot
    c$i:

    ----

    sorry i get rid of that by
    map_path[3] = "c$\0";
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  10. #10
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Well it's going somehow.!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void map_drive(char *ip, char *ipname);
    void copyfile(char *ip, char *ipname);
    
         const char drivename[]="i:";
         
    int main(void)
    {
        char ch; char line[64];
        char buf[BUFSIZ] = {'\0'}; 
        char ip[15]; 
        char ipname[64]; 
        
        FILE *fpIP;
        fpIP = fopen ("ip_adress_list.txt","rt");
        
        if (fpIP!=NULL)
        {
        
              while ( fgets(line, sizeof line, fpIP) != NULL && sscanf(line, "%15[^,], %31s", ip, ipname) == 2 )
          {
                        map_drive(ip, ipname);
          }
        
        
        /*
        // uzmi jedan red iz adress lista
           while (fgets(buf, BUFSIZ, fpIP) != NULL)
           {
                 // formatiraj red iz adress lista ip adresa i ip name
                 formatIP(buf, ip, ipname);
                 map_drive(ip, ipname);
           }                               */
        }
        else 
        {
          perror("fopen");
        }
        
        
        system("PAUSE");
        return 0;
    }
    
    void map_drive(char *ip, char *ipname)
    {
         char map_dr[50] = {'\0'};
         const char drive_path[]="e$";
         char disc_map_dr[30] = {'\0'};
         char ch;
         FILE *fpMD;
    
         fpMD = fopen("map_drive.bat", "wt");
         
         strcpy(map_dr, "net use ");
         strcat(map_dr, drivename);
         strcat(map_dr, " \\\\");
         strcat(map_dr, ip);
         strcat(map_dr, "\\");
         strcat(map_dr, drive_path);
         fprintf(fpMD, "%s\nEXIT", map_dr);
         fclose(fpMD);
         system("map_drive.bat");
    
         copyfile(ip, ipname);
         
         //DISCONNECT MAP DRIVE
         strcpy(disc_map_dr, "net use ");
         strcat(disc_map_dr, drivename);
         strcat(disc_map_dr, " /delete");
         system(disc_map_dr);    
    }
    
    void copyfile(char *ip, char *ipname)
    {
         char command[1024];
         char take_from[] = "files";
         char copy_to[] ="razno\retpos_update";
         char log_dir[] = "logs\\";
    
         strcpy(command, "robocopy ");
         strcat(command, take_from);
         strcat(command, "\\ ");
         strcat(command, drivename);
         strcat(command, "\\");
         strcat(command, copy_to);
         strcat(command, "\\ /COPYALL /E /ZB /LOG:");
         strcat(command, log_dir);
         strcat(command, ipname);
         strcat(command, ".txt");
         system(command);
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Consider sprintf rather than the strcpy-strcat chains.
    Code:
    #if 0
       strcpy(map_dr, "net use ");
       strcat(map_dr, drivename);
       strcat(map_dr, " \\\\");
       strcat(map_dr, ip);
       strcat(map_dr, "\\");
       strcat(map_dr, drive_path);
    #else
       sprintf(map_dr, "net use %s \\\\%s\\%s", drivename, ip, drive_path);
    #endif
    Code:
    #if 0
       strcpy(disc_map_dr, "net use ");
       strcat(disc_map_dr, drivename);
       strcat(disc_map_dr, " /delete");
    #else
       sprintf(map_dr, "net use %s /delete", drivename);
    #endif
    Code:
    #if 0
       strcpy(command, "robocopy ");
       strcat(command, take_from);
       strcat(command, "\\ ");
       strcat(command, drivename);
       strcat(command, "\\");
       strcat(command, copy_to);
       strcat(command, "\\ /COPYALL /E /ZB /LOG:");
       strcat(command, log_dir);
       strcat(command, ipname);
       strcat(command, ".txt");
    #else
       sprintf(map_dr, "robocopy %s \\ %s\\%s\\ /COPYALL /E /ZB /LOG:%s%s.txt",
               take_from, drivename, copy_to, log_dir, ipname);
    #endif
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Dave_Sinkula. Thank you verry much for sprintf. That help alot on reading code.
    And on sscanf as you can see. I take it.!

    I got some now ides that I wanna make so I have new question
    Now code looks like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void map_drive(char *ip, char *ipname);
    void copyfile(char *ip, char *ipname);
    void ping(char *ip);
    
         const char drive_name[]="i:";
         const char drive_path[]="e$";
    
    int main(void)
    {
        char ch; char line[64];
        char buf[BUFSIZ] = {'\0'}; 
        char ip[15]; 
        char ipname[64]; 
        
        FILE *fpIP;
        fpIP = fopen ("ip_adress_list.txt","rt");
        
        // takeing IP adresses until reach end
        if (fpIP!=NULL)
        {
        // Take one line from ip adress list and put it into coresponding variable
              while ( fgets(line, sizeof line, fpIP) != NULL && sscanf(line, "%15[^,], %31s", ip, ipname) == 2 )
          {
                        map_drive(ip, ipname);
          }
        }
        else 
        {
          perror("fopen");
        }
        
        
        system("PAUSE");
        return 0;
    }
    
    void map_drive(char *ip, char *ipname)
    {
         char map_dr[48] = {'\0'};
         char disc_map_dr[24] = {'\0'};
         
         // pinging destination for wake-ing adress
         ping(ip);
         
         //map network drive
         sprintf(map_dr, "net use %s \\\\%s\\%s", drive_name, ip, drive_path);
         puts(map_dr);
         system(map_dr);
    
         //copy files with robocopy from corespoding folder and put into LOG
         copyfile(ip, ipname);
         
         // disconect network drive
         sprintf(map_dr, "net use %s /delete", drive_name);
         puts(disc_map_dr);
         system(disc_map_dr);    
    }
    
    void copyfile(char *ip, char *ipname)
    {
         char command[254];
         char take_from[] = "files";
         char copy_to[] ="razno\retpos_update";
         char log_dir[] = "logs\\";
    
          sprintf(command, "robocopy %s \\ %s\\%s\\ /COPYALL /E /ZB /LOG:%s%s.txt", 
          take_from, drive_name, copy_to, log_dir, ipname);
    }
    
    void ping(char *ip)
    {
         char ping_c[24];
         sprintf(ping_c, "ping %s", ip);
         puts(ping_c);
         system(ping_c);   
    }
    Last edited by xxxrugby; 07-15-2006 at 10:10 AM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  13. #13
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    I wanna check if DIR == EXIST then put log into it. If DIR != EXIST then make that dir
    Do you know where can I see info with examples for
    <dirent.h> and <dir.h>?


    Second.
    when you ping he put status into console.!
    Like 4 time
    pinging xxx.xxx.xxx.xxx packet recived....
    .
    .
    .

    So I wanna catch that pinging stat and put it into log so I can check is ping goes good.!
    Do you have any ides how can I cath that.
    It work's with this also but heh
    ping xxx.xxx.xxx.xxx > out.txt
    Last edited by xxxrugby; 07-15-2006 at 11:37 AM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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