Thread: File Management (Help Please)

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    4

    Exclamation File Management (Help Please)

    Example: Please write C a program that opens a.txt which already exists and contains five 2-digit-numbers, and copies these numbers to b.txt, but with increasing order (smallest number first, largest number at the last position)! You should use a user-defined function as well!

    Example:
    a.txt="1166234587" (It means: 11, 66, 23, 45 and 87.)
    After executing the program:
    b.txt="1123456687"

    I am a student and ı am trying to figure out this problem. I can write all the numbers from one to other but ı cant sort them 2 by 2. Can someone please help me?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum. Glad to hear you started small and got the basic opening/copying part down. Can you show us what you have attempted to solve the sorting portion so far (using code tags)? Or if no code, what ideas you have come up with? We are here to help you when you get stuck on specific problems, but just offering solutions only hinders the learning process.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    What have you tried? Just thinking off the top of my head, you could probably get a function to do something like

    Code:
    int combine(int num1, int num2){
       num1*=10;
       num1+=num2;
       return num1;
    }
    Where num1 is the element initially at array[0], and num2 is initially at array[1].

    You could feed it into the function with something like:

    Edit: Sorry, gave the solution, edited out.

  4. #4
    Registered User
    Join Date
    May 2014
    Posts
    4

    There is my code.

    For now ı just copyed all numbers from one to other. But i couldnt short them for now.

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<conio.h>
    
    
    int main()
    {
     FILE *stream1,*stream2;
     char c[11]; 
     int sum=0,i=0;
    
    
     if((stream1=fopen("a.txt","rt"))==NULL)
       { printf("Error opening a.txt!");
     	fflush(stdin);getchar();exit(-1);}
    	printf("a.txt file is opened sucsessfuly\n");
    
    
     if((stream2=fopen("b.txt","w+"))==NULL)
       { printf("Error creating b.txt!");
     fflush(stdin);getchar();exit(-1);}
    	printf("File b.txt has been created.\n");
    
    
    while(fscanf(stream1,"%c",&c)!=EOF)       /* EOF=End Of File */
      {
    		
    		fprintf(stream2,"%c",c);
    	}
    	
    
    
    
    
    fclose(stream1);
    fclose(stream2);
    
    
    	return 0;
     
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, a few comments:

    You shouldn't use "fflush(stdin)" - see here -> FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    You should also work on neat, consistent indentation - see here -> SourceForge.net: Indentation - cpwiki

    You shouldn't use "conio.h" - this is non-standard, and nothing in your program requires it.

    If "stream2" doesn't open successfully, you should close "stream1" before exiting.

    You should compile at maximum warnings. This is what I got when I built your program. If you didn't see these, you need to increase the warnings on your compiler (see the documentation for instructions on how to do so).

    Code:
    /*
    main.c||In function 'main':|
    main.c|24|warning: format '%c' expects type 'char *', but argument 3 has type 'char (*)[11]'|
    main.c|27|warning: format '%c' expects type 'int', but argument 3 has type 'char *'|
    main.c|9| warning: unused variable 'i'|
    main.c|9| warning: unused variable 'sum'|
    ||=== Build finished: 0 errors, 4 warnings ===|
    */
    The first two are telling you that you're treating the varible 'c' as if it were a single character, and not an array. Rather than read the input character by character, you can read the whole line at once - see here -> FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

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

    Now onto the problem at hand.

    In general, you are approaching the code in a positive way. Building up the program piece by piece and testing as you go along. This is a very smart way to write programs.

    Before you go any further with the code, you should plan out the logic you need to implement, by hand (i.e. pencil and paper). Break the problem down into small steps, and work on solving them one at a time.

    One way to approach this would be to:
    - read the input (already done)
    - break up the string into separate two-digit values
    - store these values somewhere (an int array would do nicely)
    - sort these values
    - print them to the output

    Think about these steps, and see if you can come up with any solutions. Just try to solve one problem at a time. Continue adding code in small chunks, compiling, and testing. Try to build up the program in this manner, and let us know if and where you get stuck.

  6. #6
    Registered User
    Join Date
    May 2014
    Posts
    4
    Thank you very much for your answer. I am blowed my mind. I am working about 6 hour for this project and ı couldnt get any positive resoult. First of all i dont know how to break up string into separete two digit values.

    Second one is when i try to sort my numbers they are changing. I guess it is because of ı am taking every char 1 by 1. But when ı am saying char c[100] my numbers shows up just EEEEE and nothing else.

    I can read program and understand how it work but when i need to write ı am stucking.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Know how I said that you should break the problem down into smaller steps? If one of those steps is still too difficult, you can break that one step down even further.

    Let's zoom further into the next step ("break up the string into separate two-digit values"), shall we?

    First, realize that by reading the input as a string, each "number" is actually character symbol and not the actual integer itself. One of the ways you can convert a single character "digit" into a number is to subtract the the value of the character zero.

    There are many character sets, but one of the most common is based on ASCII. You can find the table of those values here -> Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Now let's say you had the character symbol (not the number) '7'. According to the table, character '7' has the value of decimal 55. The character '0' has the value of decimal 48.

    55 - 48 = 7

    Or, to put it another way:

    '7' - '0' = 7

    Feel free to create a new "test" program and play around with this math.

    So now you know how to convert a character symbol "number" to the actual number itself.

    The next step would be how to use two of these integers to arrive at a two-digit number. The math for this has already been illustrated in this thread by someone else.

    Armed with this knowledge, you should be able to reason out the steps needed to extract all the two digit numbers from the input string.

  8. #8
    Registered User
    Join Date
    May 2014
    Posts
    4
    I know this convertings and ASCII codes. But ı have problem about applying them. I can understand better if you can show me this problem with c programming codes. But when ı am building this program it gives me some errors and ı cant figure it out. İf you can write me this program all from begining to end ı can ask you places whic ı dont understand and ı can investigate it.

    Now i am trying new Alghrotihm. I closed the bouth file after read and copy it and ı opened b.txt file again as a append file. So now i am trying to get every character and sort them. But it gives me problem. Time is late and i am tired. I will continue tomorrow. Thank you for spending time for me. But as i told. ıf you can write this code for me it will be more helpfull for me.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by İlkay İvrendi View Post
    I know this convertings and ASCII codes. But ı have problem about applying them. I can understand better if you can show me this problem with c programming codes. But when ı am building this program it gives me some errors and ı cant figure it out. İf you can write me this program all from begining to end ı can ask you places whic ı dont understand and ı can investigate it.

    Now i am trying new Alghrotihm. I closed the bouth file after read and copy it and ı opened b.txt file again as a append file. So now i am trying to get every character and sort them. But it gives me problem. Time is late and i am tired. I will continue tomorrow. Thank you for spending time for me. But as i told. ıf you can write this code for me it will be more helpfull for me.
    You're welcome for the assistance, but I will not be writing the code for you. First of all, it goes against the homework policy. Secondly, contrary to popular belief, reading code written by someone else does practically nothing to help beginners learn, especially compared to struggling oneself and figuring it out on your own. We offer help and suggestions, and you try to implement it.

  10. #10
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @Matticus - Well, that was way simpler than my solution of storing the number in a long long int, and extracting the digits into an array through modular division, then combining them into a pair through what I posted above. It worked, but was far more complicated that converting the character values.

    To the OP, it looks like there are multiple solutions, just focus on getting the actual numerical values into an array. From there it is a cinch to combine them into 2 digit values.
    Last edited by Alpo; 05-19-2014 at 07:06 PM.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Alpo View Post
    @Matticus - Well, that was way simpler than my solution of storing the number in a long long int, and extracting the digits into an array through modular division, then combining them into a pair through what I posted above. It worked, but was far more complicated that converting the character values.
    As long as the constraints of the assignment hold true, there is an even more elegant (one line within a loop) solution for extracting and storing the digits (assuming the quotes were not part of the file data). I didn't want to lead the OP in that direction, though, for a few reasons - it would be difficult to describe without just giving away the solution, and it's not a solution that a beginner might come up with on their own (which could look bad for them if it's a school assignment).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File management, help please!
    By zetaXX in forum C Programming
    Replies: 8
    Last Post: 06-03-2013, 12:05 PM
  2. file management in c help
    By coklat91 in forum C Programming
    Replies: 1
    Last Post: 12-19-2011, 09:23 PM
  3. file management in c
    By Animesh Gaitond in forum C Programming
    Replies: 4
    Last Post: 09-20-2011, 10:28 AM
  4. File Management in C
    By Figen in forum C Programming
    Replies: 6
    Last Post: 01-31-2011, 03:14 PM
  5. need help for file management
    By edesign in forum C Programming
    Replies: 5
    Last Post: 02-08-2008, 11:29 PM