I was looking for a tutorial on how to create files. I'm real new to C programming. I have to create a program which uses two files for input. I just need the create the files and then I can hammer out the rest. I know how to access the files just not how to create them.

Does it require the use of fprintf?

The files can be created with fopen() function. Here is a description of the function (not a tutorial). There isn't really much to it: To open for output (destroys the fle if it already exists).

FILE* fp = fopen("myfile.txt","w");

I want to read a file account with customer id and balance in it. Another file has transaction with transaction id, cust id, transaction(0 for sale 1 for credit) , and amount to change balance by. Running into errors. Please help or send me to where I can receive it.

#include<stdio.h>
#define ACCTMAX 89999
#define TRANSMAX 8999999

typedef struct{
       int custid[6];
       int balance[10];
}CUSTINFO;

typedef struct{
       int transid[8];
       int custid[6];
       int trans[2];
       int amount[10];
}TRNSINFO;

int readCustArray (FILE *fp,CUSTINFO a[]){
       int stat1,stat2;
       int i;
       for(i=0;i<ACCTMAX;i++){
               if (stat1==EOF)
                       break;
               stat1 = fscanf(fp, "%d", &a[i].custid);
               stat2 = fscanf(fp, "%d", &a[i].balance);
       }
       return i;
}

int readTransArray (FILE *fp, TRNSINFO a[]){
       int stat1,stat2,stat3,stat4;
       int i;
       for(i=0;i<TRANSMAX;i++){
               if (stat1==EOF)
                       break;
               stat1 = fscanf(fp,"%d",&a[i].transid);
               stat2 = fscanf(fp,"%d",&a[i].custid);
               stat3 = fscanf(fp,"%d",&a[i].trans);
               stat4 = fscanf(fp,"%d",&a[i].amount);
       }
       return i;
}

void printCustArray(CUSTINFO x[],int l, int r){
       if (l<=r){
               printf("%d",x[l].custid);
               printf("%d\n",x[l].balance);
               printCustArray(x,l=1,r);
       }
}

void swapCust (CUSTINFO x[], int a, int b){
       int t1,t2;
       t1 = x[a].custid;
       x[a].custid=x[b].custid;
       x[b].custid=t1;
       t2 = x[a].balance;
       x[a].balance=x[b].balance;
       x[b].balance=t2;
}

void swapTrans(TRNSINFO x[], int a, int b){
       int t1,t2,t3,t4;
       t1=x[a].transid;
       x[a].transid=x[b].transid;
       x[b].transid=t1;
       t2=x[a].custid;
       x[a].custid=x[b].custid;
       x[b].custid=t2;
       t3=x[a].trans;
       x[a].trans=x[b].trans;
       x[b].trans=t3;
       t4=x[a].amount;
       x[a].amount=x[b].amount;
       x[b].amount=t4;
}

int findCustSmallest(CUSTINFO x[],int l,int r){
       int minOfRest;
       if (l>r)
               return -1;
       else if (l==r)
               return l;
       else {
               minOfRest=findCustSmallest(x,l+1,r);
               return x[l].custid<=x[minOfRest].custid?l:minOfRest;
       }
}

int findTransSmallest(TRNSINFO x[],int l,int r){
       int minOfRest;
       if (l>r)
               return -1;
       else if (l==r)
               return 1;
       else {
               minOfRest=findTransSmallest(x,l+1,r);
               return x[l].custid<=x[minOfRest].custid?l:minOfRest;
       }
}

void sortCust(CUSTINFO a[],int l, int r){
       int p;
       if (l>=r)
               return;
       else {
               p=findCustSmallest(a,l,r);
               swapCust(a,l,p);
               sortCust(a,l+1,r);
       }
}

void sortTrans(TRNSINFO a[],int l, int r){
       int p;
       if (l>=r)
               return;
       else{
               p=findTransSmallest(a,l,r);
               swapTrans(a,l,p);
               sortTrans(a,l+1,r);
       }
}

int bsearch(CUSTINFO a[], int l,int r, int custid){
       int m;
       if(l>r)
               return -1;
       else {
               m=(l+r)/2;
               if(custid<a[m].custid)
                       return bsearch(a,l,m-1,custid);
               else if (custid>a[m].custid)
                       return bsearch(a,m+l,r,custid);
               else
                       return m;
       }
}

int main () {
       CUSTINFO x[ACCTMAX];
       TRNSINFO y[TRANSMAX];
       int l = 0;




       return 0;
}

There are several errors in your program. The first error is in swapCust().

t1 = x[a].custid;

custid is an int array, so attempting to assign t1 like that is illegal. you have to get a specific element, like this, where i contains the element number you want.

I guess the intent of that function is to swap the entire structure. That can be easily done like below, where it is not necessary to swap the individual structure members, such swap the entire structure at one time.

void swapCust (CUSTINFO x[], int a, int b){
	   CUSTINFO temp = x[a];
	   x[a] = x[b];
	   x[b] = temp;
}

Now do the same thing with function swapTrans().

Why do those two structures contain int arrays ? Your program is attempting to use them as if they were simple ints. I think they should look like this:

typedef struct{
       int custid;
       int balance;
}CUSTINFO;

typedef struct{
       int transid;
       int custid;
       int trans;
       int amount;
}TRNSINFO;
This article has been dead for over six months. Start a new discussion instead.