Thread: Transferring files between Windows and Unix

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    Transferring files between Windows and Unix

    Hello everyone! I need help with a problem i'm having in this program. I'd appreciate any help.
    Here's the requirements i have to meet:
    «Write a program that reads a text file and rewrites it on another file such that every character in each line is writen in the reversed order - that is to say in each new line, the last character will be the first, etc...
    Note 1 - To simplify take into account that the file that will be read won't have lines with more than 1024 characters, including the terminator. Furthermore, allocate the string you will use.
    Note 2-You also have to notice that the line in a unix file ends with the character 10(LF), whereas the lines in a MS/DOS file ends with the characters 13 and 10 (CR LF).»
    Given that, here's the program i wrote, and i'd like for you to tell me what is missing, and how can i do the last step, written in Note 2, to properly transfer a file between unix and Windows. Thank you very much.
    Heres the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main ()
    {
        int i=0;
        int l;
        FILE *f1;
        FILE *f2;
        char str[1024],b, a;
    
    
        f1=fopen("c:\\.ReadingFiletxt", "rt");
        if (!f1)
            printf ("Error\n");
        f2=fopen("c:\\WritingFile.txt", "wt");
        while (fgets(str, 1024,f1) != NULL)
            {
                l=strlen(str);
                a=malloc(l*sizeof (char));
                i=0;
                while(i < l/2)
                {   b=str[i];
                    str[i]=str[l-1-i];
                    str[l-1-i]=b;
                    ++i;
                }
                    printf("%s", str);
    
    
            }
    
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You simply need to check the second-to-last character to seem if it's a CR. If it is, skip that as you skip LF.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    I'm sorry, but could you explain how can i exactly do that? I am new to programming, and still haven't quite yet grasped the concepts related to the ASCII table, or CR and LF for that matter.
    Just one more thing, how can i allocate the string memory if i already define the string with the memory str[1024]?
    Much appreciated!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Before your inner while you, you just need to do:
    Code:
    if (str[l-1] == '\n') { // Ignore LF
        --l;
    }
    if (str[l-1] == '\r') { // Ignore CR
        --l;
    }
    how can i allocate the string memory if i already define the string with the memory str[1024]
    By using malloc(l+1) and saving the returned pointer to the correct variable. 'a' isn't a pointer. Then use strcpy() to copy the string. Also, you should always free() memory after you're done with it. But you don't need to do all that, simply print the string directly into the output file.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Ok, that makes sense, though i'm still having trouble allocating the memory. Here's what i changed, given that if i allocated the memory for 'str' it would appear 'error:assignment to expression with array type'. However trying the strcpy() for the 'a' as a pointer, the program won't run. One more thing, instead of using '\n' and '\r', i used the decimal equivalent of 10 and 13, is it ok to do it like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main ()
    {
        int i, l;
        FILE *f1;
        FILE *f2;
        char str[1024], b, *a;
    
    
        f1=fopen("File1", "rt");
        if (!f1)
            printf ("Error\n");
    
        f2=fopen("File2", "wt");
        if (!f2)
            printf("Error\n");
    
        while (fgets(str, 1024,f1) != NULL)
            {
                l = strlen(str);
                a = malloc((l+1)*sizeof (char));
                i=0;
    
                if (str[l-1] == 10)
                    --l;
    
                if (str[l-1] == 13)
                    --l;
    
                while(i < l/2)
                  {
                    b=str[i];
                    str[i]=str[l-1-i];
                    str[l-1-i]=b;
                    ++i;
                  }
    
                printf("%s", str);
                fprintf(f2, "%s", str);
    
            }
        free(a);
        fclose(f1);
        fclose(f2);
    
    
        return 0;
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by user_0x View Post
    However trying the strcpy() for the 'a' as a pointer, the program won't run.
    What do you mean? Does it compile but crashes or doesn't compile at all?

    Quote Originally Posted by user_0x View Post
    One more thing, instead of using '\n' and '\r', i used the decimal equivalent of 10 and 13, is it ok to do it like this?
    It's so and so. You see, this is what we call "magic numbers", meaning that you're using seemingly arbitrary values. Not all people care to remember or know what the ASCII values for LF and CR are, and won't easily recognize that you're checking for the end of the line.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    What appears to happen is, since im using the CodeBlocks app, whenever i build and run the code, the terminal pops up, without anything written in it, then another box appears disclosing «The program has stopped working.» and it closes the terminal.
    It doesn't really point out any error, just closes everytime i run it.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    When the file fails to open it is a good idea to call exit(1) to quit the program.
    Replace the 1 with any non-zero value you want.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Ok, i might try that, but the problem only arised when i inserted that malloc argument, that is to say, when i replaced the initial 'a' with the pointer '*a', and then after using strcpy(a,str); between the lines of malloc and strlen, it started appearing this error. Could you tell me if the allocation is correct or not? And if not, how can i ammend it? One more thing, is there a need to free() the memory allocated? And if so do i insert it in the while cicle or at the end of the code?
    Thank you very much!

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    something like this maybe?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_BUFF 1024
    
    int main (int argc, const char **argv)
    {
    char backwords[MAX_BUFF];
    
        FILE *fp;
       
        fp = fopen(argv[1], "r");
        
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        } 
        
        int c ,len,cnt = 0;
     
     //put into array
        while ( (c = fgetc(fp) ) != EOF)
        {
            backwords[cnt++] = c;
        }
     
        len = strlen(backwords);
    //this code prints everything backwards
    // bottom to top
    
    for (; len>=0;len--)
    {    
         
        
        if(backwords[len] == 10)            
                printf("\n");
            else
                printf("%c",backwords[len]);
    }
    printf("\n\n\n");
    
    char line[1024];
    // prints each line backwards top to bottom
    //
    len = strlen(backwords);
    cnt = 0; //reset
    for( int a =0;a<len;a++)
    { //use cnt to add amount
     line[cnt++] = backwords[a];
     
        if (backwords[a] == 10)
        {
            for (; cnt>=0;cnt--) //resets cnt to zero at the same time
            {
                printf("%c",line[cnt-1]);
            }
        //    printf("\n");
        }
        
     }
    //may be needing to zero out the line array for block out over run
    // of last line put into that array. for safety measures. 
    fclose(fp);
    
    return 0;
    }
    test file and output
    Code:
    One more thing, is there a need to free() the memory allocated? 
    And if  so do i insert it in the while cicle or at the end of the code?
    Thank you very much!
    
     ?detacolla yromem eht )(eerf ot deen a ereht si ,gniht erom enO
    ?edoc eht fo dne eht ta ro elcic elihw eht ni ti tresni i od os fi dnA
    !hcum yrev uoy knahT
    still needs clean up code and windows end line and whatever else you deem necessary
    K.I.S.S.
    Last edited by userxbw; 11-19-2017 at 03:06 PM.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by user_0x View Post
    What appears to happen is, since im using the CodeBlocks app, whenever i build and run the code, the terminal pops up, without anything written in it, then another box appears disclosing «The program has stopped working.» and it closes the terminal.
    It doesn't really point out any error, just closes everytime i run it.
    terminal popping up is what CB does with cli programs it is somewhere in the settings I think you can turn it off. I use to just open up a separate terminal and run my programs in that. I think that is why I see system (pulse ) in some code here and there. to keep the terminal open. (maybe)

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    updated I missed that you got a make it on the fly
    using some of your methodology this is what I came up with, eliminating check for end line, it works in Linux. Windows Donno'
    takes first string, malloc its size copy first string to it, then run that backwards into a file, get next string in file repeat until eof, or NULL as the case maybe.
    Code:
    rewind(fp);
    char line_read[MAX_BUFF], *bk_str;
    
    FILE *fp1;
     
      if ( (fp1 = fopen("results", "w+") )== NULL)
            fp1 = fopen("results", "w+");
             
    while (fgets(line_read, 1024,fp) != NULL) 
    {
            /*
            len = strlen(line_read);
            bk_str = malloc((len+1)*sizeof(char));
            strcpy(bk_str,line_read);
            */
         
            bk_str = malloc(( (len = strlen(line_read)) +1)*sizeof(char));
            strcpy(bk_str,line_read);
        
            
            for(; len >=0;len--)
                fprintf(fp1,"%c",bk_str[len]);
       
        //MOD here
    
        free(bk_str);
    }
      
    //free(bk_str);
    fclose(fp1);
    fclose(fp);
    results file reads.
    Code:
    userx@slackwhere:~/bin
    $ cat results
    
     ?detacolla yromem eht )(eerf ot deen a ereht si ,gniht erom enO
    ?edoc eht fo dne eht ta ro elcic elihw eht ni ti tresni i od os fi dnA
    !hcum yrev uoy knahT
    Last edited by userxbw; 11-19-2017 at 08:13 PM.

  13. #13
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by userxbw View Post
    updated I missed that you got a make it on the fly
    Code:
    rewind(fp);
    char line_read[MAX_BUFF], *bk_str;
    
    FILE *fp1;
     
      if ( (fp1 = fopen("results", "w+") )== NULL)
            fp1 = fopen("results", "w+");
             
    while (fgets(line_read, 1024,fp) != NULL) 
    {
            len = strlen(line_read);
            bk_str = malloc((len+1)*sizeof(char));
            strcpy(bk_str,line_read);
        
            
            for(; len >=0;len--)
                fprintf(fp1,"%c",bk_str[len-1]);
    }
      
    free(bk_str);
    fclose(fp1);
    fclose(fp);
    results file reads.
    Code:
    userx@slackwhere:~/bin
    $ cat results
    
     ?detacolla yromem eht )(eerf ot deen a ereht si ,gniht erom enO
    ?edoc eht fo dne eht ta ro elcic elihw eht ni ti tresni i od os fi dnA
    !hcum yrev uoy knahT
    You might want to fix the memory leaks

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    You might want to fix the memory leaks
    yeah I've been sitting here wondering about that, it get a new pointer to bk_str each malloc, some how needs to get separated? hold on,
    this did not work
    Code:
    rewind(fp);
    char line_read[MAX_BUFF], *bk_str;
    
    FILE *fp1;
     
      if ( (fp1 = fopen("results", "w+") )== NULL)
            fp1 = fopen("results", "w+");
             
    while (fgets(line_read, 1024,fp) != NULL) 
    {
      // both of these methods work. 
            /*
            len = strlen(line_read);
            bk_str = malloc((len+1)*sizeof(char));
            strcpy(bk_str,line_read);
            */
         //get length malloc size of line then copy
            bk_str = malloc(( (len = strlen(line_read)) +1)*sizeof(char));
            strcpy(bk_str,line_read);
        
        // run that line backwards into file    
            for(; len >=0;len--)
                fprintf(fp1,"%c",bk_str[len]);
                
            // now free it     
            free(bk_str);
    }
     
    //free(bk_str);
    Now I think it do, I did not have that last free commented out when I tried this, and was getting error,
    Code:
    7fd7ff1cb000-7fd7ff1cc000 r--p 00025000 08:05 1190644                    /lib64/ld-2.23.so
    7fd7ff1cc000-7fd7ff1cd000 rw-p 00026000 08:05 1190644                    /lib64/ld-2.23.so
    7fd7ff1cd000-7fd7ff1ce000 rw-p 00000000 00:00 0 
    7ffd8062d000-7ffd8064e000 rw-p 00000000 00:00 0                          [stack]
    7ffd806fa000-7ffd806fc000 r--p 00000000 00:00 0                          [vvar]
    7ffd806fc000-7ffd806fe000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    Aborted
    I didn't scroll back up to top to see what it actually said until I just tried to reproduce this error. then commented out that second free.
    Last edited by userxbw; 11-19-2017 at 08:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with unix/windows sockets.
    By thylacinelol in forum C Programming
    Replies: 3
    Last Post: 10-08-2013, 11:57 AM
  2. C++ Code : Unix vs Windows
    By mrJTparadise in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2012, 01:13 AM
  3. Connecting windows through Unix
    By vin_pll in forum Tech Board
    Replies: 10
    Last Post: 06-23-2008, 10:32 PM
  4. query regarding transferring large files across network
    By arron in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-12-2005, 10:53 AM
  5. unix, windows
    By stef in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 03:37 PM

Tags for this Thread