GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Mar-2007, 04:03
jamesbond000 jamesbond000 is offline
New Member
 
Join Date: Mar 2007
Location: India
Posts: 4
jamesbond000 is on a distinguished road

File function


hi friends,

this is the question but i m not continue what shall to proceed and errors occur i m not familiar with c program use of file can u try it please.

Write an interactive program that reads three (3) lists of numbers, which are stored in three separate files, and creates one (1) sorted list. Each file should contain not more than 15 numbers. First, you need to create a program that randomly chooses a size (<= 15 numbers) for the list1 and then randomly generates a list of numbers and store them in the file1. Repeat this procedure to select the other two lists (list2 and list3) and stores them in the corresponding files (file2 and file3) for sorting and merging them into one.

for example:

file1(list 1) 32 12 5 990 1

file2 (list 2) 2000 3

file3 (list 3) 30 6

Output in a separate file:
1 3 5 6 12 30 32 990 2000


code:
CPP / C++ / C Code:
#include"stdio.h"
void main(){
FILE *fp1,*fp2,*fp3,fp4;
char ch1;
int temp_arr[50];
int i=0;
int limit;

fp1=fopen("file1","w");
fp2=fopen("file2","w");
fp2=fopen("file3","w");

printf("\nEnter nums for file1: ");
for(i=0;i<15;i++)
{
scanf("%d",&temp_arr[i]);
ch1=(char)temp_arr[i];
 fputc(ch1,fp1);

    }

printf("\nEnter nums for file2: ");
for(i=0;i<15;i++)
{
 scanf("%d",&temp_arr[i]);
 ch1=(char)temp_arr[i];
 fputc(ch1,fp2);

 }

printf("\nEnter nums for file3: ");
for(i=0;i<15;i++)
{
scanf("%d",&temp_arr[i]);
ch1=(char)temp_arr[i];
fputc(ch1,fp3);

}

fclose(fp1);
fclose(fp2);
fclose(fp3);

fp1=fopen("file1","r");
fp2=fopen("file2","r");
fp3=fopen("file3","r");
fp4=fopen("result","w");

while((ch1=fgetc(fp1))!=EOF){ temp_arr[i++]=(int)ch1;}
while((ch1=fgetc(fp2))!=EOF){ temp_arr[i++]=(int)ch1;}
while((ch1=fgetc(fp3))!=EOF){ temp_arr[i++]=(int)ch1;}


limit = i;
i=0;
while(limit>0)
{
fwrite(fp4,"%d",temp_arr[i++])
limit-=1;
 }
}


Please can anyone try it and whereever require code please insert it and the numbers are not sorted can u try it


thks in advance
Last edited by LuciWiz : 29-Mar-2007 at 00:07. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 28-Mar-2007, 12:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: file function


Quote:
Originally Posted by jamesbond000
i m not familiar with c program use of file

I will give some suggestions. Not everyone would do things this way and in this order, but you might consider the following:

First of all, before trying to write and read multiple files and performing a merge/sort, get familiar with use of file I/O in C.

1. The code that you posted will not compile, for a number of reasons.

2. I suggest that you get familiar with file I/O by making a program that will read each and every character from a text file (that you created with a text editor) and print each character to the console. Make sure that it reads exactly what is in the file: no more and no less.

3. Then make a program that reads each and every character from a text file and writes each one to another file. Make absolutely that the end result is that every character from the input file is in the output file, and that the files have exactly the same size.

Notes:

Every time you do an fopen(), make sure that the file was successfully opened. Print out a message each time.

If you are just going to read characters, then fgetc() is appropriate. If you are going to read integers, then fscanf() might be the way to go (at least for starters). If you use fscanf(), make sure that you test each time to make sure that the conversion from file to integer value was successful.

You can read up on these functions (and a lot of other good stuff) at sites like this: Standard C



I'll get you started with a program that opens a file and counts the total number of bytes in the file:

CPP / C++ / C Code:
/* program to count characters in a text file */
#include <stdio.h>

int main()
{
    FILE *infile;
    char *inname = "test.txt";
    int inchar; /* note that this is an int; not a char */
    int counter = 0;

    infile = fopen(inname, "r");
    if (infile == NULL) { 
        printf("There was a problem reading file %s for reading\n", inname);
        return 0;
    }
    else {
        printf("File %s opened for reading\n", inname);
        while ((inchar = (fgetc(infile) != EOF))) {
            ++counter;
        }
        printf("Total number of bytes in %s = %d\n", inname, counter);
    }
    return 0;
}

Output on my Windows XP platform:

Code:
F:\temp>dir . . . Directory of F:\temp . . 03/28/2007 11:46 AM 3,584 countchars.exe 03/28/2007 11:46 AM 643 test.txt . . . F:\temp>countchars File test.txt opened for reading Total number of bytes in test.txt = 643

After making sure you can read each and every byte, then create a file (with a text editor) that contains the kind of entries your program will be using (integers, I guess). Make sure your program can read every integer from the file and print its value on the screen.

Then, start all over with a brand new program that will simply write a few integers to a text file. Look at the file with a text editor after you have run the program. Make absolutely sure that you can write exactly what you intend to write: no less, no more.

etc.
etc.

Approach the problem logically. Make absolutely sure that you implement and test each of the required steps before trying to put them all together.


Regards,

Dave
  #3  
Old 28-Mar-2007, 22:03
jamesbond000 jamesbond000 is offline
New Member
 
Join Date: Mar 2007
Location: India
Posts: 4
jamesbond000 is on a distinguished road

Re: file function


ok thanks for help what can i do i want to change the code r it can update in this above code as i posted.

davekw7x he/she given an example one to find the character in the text file but i want to write in those file and read it and after that i want to sorted the integer numbers. can u help me nw.
  #4  
Old 28-Mar-2007, 22:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: file function


Quote:
Originally Posted by jamesbond000
ok thanks for help what can i do i want to change the code r it can update in this above code as i posted.

davekw7x he/she given an example one to find the character in the text file but i want to write in those file and read it and after that i want to sorted the integer numbers. can u help me nw.

One thing at at time!

Make a program that writes integers to a file. Where do you get the integers? How many integers? That's part of the assigmnent and part of the program.

You are going to create a text file. Write one integer on each line. You can write a single integer, n, followed by a newline to stdout with printf("%d\n", n). If you have opened a file for writing, then you write a single integer followed by a newline with fprintf(outfile, "%d\n", n).

Then write a program that reads integers from a text file. You can read an integer from stdin with scanf("%d", &n) YOu can read an integer from a text file with fscanf(infile, "%d, &n) Look for examples in your textbook or other course materials.

After you know that you can create the files and can read each file, then you need to plan how to do the merge/sort of the three files.

But: first things first: make sure you understand how to write and read files. If you write a program that doesn't work as expected, then post the program and ask specific questions about the behavior that you don't understand.

Bottom line: I am not going to write the program; you are.

Regards,

Dave
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:42.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.