Προς το περιεχόμενο

Δημιουργία αρχείου δεδομένων στην C


mike...

Προτεινόμενες αναρτήσεις

Δημοσ.

Είναι ένα πρόγραμμα που δημιουργεί ένα αρχείο κειμένου και μετρά τη συχνότητα εμφάνισης κάθε χαρακτήρα a-z A-Z 0-9. Για την καταμέτρηση να χρησιμοποιηθεί δομή. Κατόπιν θα αποθηκεύει σε ένα νέο αρχείο το μικρό ,το κεφαλαίο γράμμα και τον αριθμό με την μεγαλύτερη συχνότητα εμφάνισης. Στην επιλογή 4 αν ο χρήστης τροποποιήσει το αρχείο κειμένου πρέπει να ενημερώνεται το αρχείο αποτελεσμάτων για τις αλλαγές.

 

>#include <stdio.h>
#include <string.h>
#define a 36
#define n 26
#define m 10
struct text
{
int word[n];
int num[m];
};
typedef struct text tab;
void createfile(FILE *f);
void calcwords(FILE *f,tab p[a]);
void savefile(FILE *f);
void write(FILE *f);
main()
{
struct text pin[a];
int choice,calc;
FILE *fp;
fp=fopen("text.txt","w+");
do
{
printf(" 1- Dimiourgia arxeiou dedomenon\n 2- Ypologismos kai efmanisi ton grammaton ton sixnotiton tous stin othoni\n 3- Apothikeusi apotelesmatos se arxeio\n 4- Tropopoisisi arxeiou dedomenon\n 5- Exodos\n\n ");
scanf("%d",&choice);
if (choice==1)
createfile(fp);
else if (choice==2)
calcwords(fp,pin);
else if (choice==3)
savefile(fp);
else if (choice==4)
{
write(fp);
calcwords(fp,pin);
}
}
while (choice!=5);
return 0;
}
void createfile(FILE *f)
{
char file[200];
fscanf(f,"%s",file);
}
void calcwords(FILE *f,tab p[a])
{
int i;
char ch;
ch=getc(f);
if(ch>='a' && ch<='z')
{
i=ch-'a';
p[i].word[i]++;
}
for(i=0;i<n;i++)
{
if(p[i].word[i]!=0)
{
  printf("\n%c : %d",i+'a',p[i].word[i]);
}
}
if(ch>='0' && ch<='9')
{
i=ch-'0';
p[i].num[i]++;
}
for(i=0;i<m;i++)
{
if(p[i].num[i]!=0)
{
  printf("\n%c : %d",i+'a',p[i].num[i]);
}
}
}
void savefile(FILE *f)
{
fclose(f);
}
void write(FILE *f)
{
char file[200];
fprintf(f,"%s",file);
fscanf(f,"%s",file);
}

 

μπορεί να βοηθήσει κάποιος με το πρόγραμμα?

βασικά το πρόβλημα που έχω είναι στην δημιουργία του αρχείου

Δημοσ.

Το παρακάτω πρόγραμμα είναι γραμμένο σε VC++ExprEdition2008 και υπολογίζει την συνχότητα των γραμμάτων σε ένα αρχείο κειμένου καθώς επίσης και δημιουργεί ένα νέο αρχείο κειμένου με τα αποτελέσματα των μέγιστων συχνοτήτων. Το πρόγραμμα είναι βασισμένο πάνω σε δομές, το μόνο που θα χρειαστεί από εσένα ίσως είναι κάποιες αλλαγές στην main πάνω στα δεδομένα του προγράμματος. Καλή επιτυχία, παρουσιάζονται και εκτυπωμένα αποτελέσματα:

 

>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>

static char *mode[] = {"rt", "wt"};

typedef struct _LetterFreq
{
int *az;
int *AZ;
int *ZeroToNine;
}LetterFreq;

LetterFreq *Init(int naz, int nAZ, int nZN)
{
LetterFreq *let = NULL;
if((let = calloc(1, sizeof(LetterFreq))) == NULL)
{
	fprintf(stderr,"Calloc: %s\n", strerror(errno));
	return NULL;
}
let->az = calloc(26, sizeof(int));
   let->AZ = calloc(26, sizeof(int));
let->ZeroToNine = calloc(10, sizeof(int));
	return let;

free(let); let = NULL;
return NULL;
}

int DeleteLetter(LetterFreq *l)
{
if(l)
{
	if(l->AZ != NULL && l->az != NULL && l->ZeroToNine != NULL)
	{
		free(l->AZ); l->AZ = NULL;
		free(l->az); l->az = NULL;
		free(l->ZeroToNine); l->ZeroToNine = NULL;
	}
	free(l);
	l = NULL;
	return 1;
}
return 0;
}

int ReadFromFile(const char *Filename, LetterFreq **letFreq)
{
if(!Filename)
{
	fprintf(stderr,"Filename Error %s\n", strerror(errno));
	return -1;
}
else
{
	static int i = -1;
	FILE *f = NULL;
	*letFreq = Init(26,26,10);
	if(*letFreq)
	{
		if((f = fopen(Filename, mode[0])) == NULL)
		{
			fprintf(stderr,"File Error %s\n", strerror(errno));
			DeleteLetter(*letFreq);
			return -2;
		}
		else
		{
			char Buffer[bUFSIZ] = "";
			while((fgets(Buffer, sizeof(Buffer), f)) != NULL)
			{
				if(Buffer[strlen(Buffer)-1] == '\n')
					Buffer[strlen(Buffer)-1] = '\0';
				for(i = 0; i < strlen(Buffer); i++)
				{	
						if(isdigit((int)Buffer[i]))
							(*letFreq)->ZeroToNine[(int)Buffer[i] - 48]++;
						if(Buffer[i] >= 'A' && Buffer[i] <= 'Z')
							(*letFreq)->AZ[(int)Buffer[i] - 65]++;
						if(Buffer[i] >= 'a' && Buffer[i] <= 'z')
							(*letFreq)->az[(int)Buffer[i] - 97]++;
				}
				memset(Buffer, 0 , sizeof(Buffer));
			}
			fclose(f);
			return 1;
		}
	}
	else
	{
		fprintf(stderr,"{NULL}\n");
		return -3;
	}
}
}
void Print(LetterFreq *f)
{
static int i = -1;
char A = 'A';
char a = 'a';
char zero = '0';
for(i = 0; i < 26; i++)
	printf("%c:%d ", A++, f->AZ[i]);
printf("\n\n");
for(i = 0; i < 26; i++)
	printf("%c:%d ", a++, f->az[i]);
printf("\n\n");
for(i = 0; i < 10; i++)
	printf("%c:%d ", zero++, f->ZeroToNine[i]);
printf("\n\n");
}

int FindMax(int *A, int SZ)
{
int i = 0, Index = -1;
int MAX = A[0];
for(i = 0; i < SZ; i++)
{
	if(MAX < A[i])
	{
		MAX = A[i];
		Index = i;
	}
}
return Index;
}

int WriteToFile(const char *filename, LetterFreq *let, int STDout)
{
if(filename)
{
	FILE *f = NULL;
	int szVal = -1;
	if(!STDout && (f = fopen(filename, mode[1])) == NULL)
		return -1;
	else
	{
		char A = 'A', a = 'a', Zero = '0';
		if(fprintf(f = !STDout ? f : stdout, "AZ_MAX{%c}=%d\naz_MAX{%c}=%d\n0-9_MAX{%c}=%d", A += FindMax(let->AZ, 26) >= 0 ? FindMax(let->AZ, 26) : -35,let->AZ[FindMax(let->AZ, 26)], a += FindMax(let->az, 26) >= 0 ? FindMax(let->az, 26) : -62,let->az[FindMax(let->az,26)], Zero += FindMax(let->ZeroToNine, 10) >= 0 ? FindMax(let->ZeroToNine, 10) : -13,let->ZeroToNine[szVal = FindMax(let->ZeroToNine,10) < 0 ? 0 : FindMax(let->ZeroToNine,10) ]) < 0)
		{
			fprintf(stderr,"Write Error.\n");
			return -2;
		}
		fclose(f);
		return 0;
	}
}
else
{
	fprintf(stderr,"{NULL}:%s\n", strerror(errno));
	return -3;
}
}

int main(int argc, char *argv[])
{
int i = -1;
int RDF = 0;
LetterFreq *f = NULL;
RDF = ReadFromFile("test1.txt", &f) < 0 ? RDF : !RDF;
RDF > 0 ? WriteToFile("results.txt", f, 1) : !RDF;
if(RDF)
	DeleteLetter(f);
printf("Hit enter to continue....\n");
getchar();
return 0;
}

 

Σε περίπτωση που δεν βρεθεί κανένα από τα γράμματα μέσα στο κείμενο τότε το πρόγραμμα εμφανίζει τα αποτελέσματα με τον χαρακτήρα{#} = 0 άρα μηδέν για τα σύμβολα μας. Επίσης το πρόγραμμα προβλέπει ταυτόχρονα την παρουσίαση αποτελεσμάτων τόσο στο εξωτερικό αρχείο "results.txt" αλλά και στο stdout με αλλαγή της μεταβλητής από 0->1 στο "RDF ? WriteToFile("results.txt", f, 0) : !RDF;" στην main δηλαδή σε RDF ? WriteToFile("results.txt", f, 1) : !RDF;

 

Αρχείο είσοδος κειμένου:

 

New blood joins this earth

And quikly hes subdued

Through constant pain disgrace

The young boy learns their rules

 

With time the child draws in

This whipping boy done wrong

Deprived of all his thoughts

The young man struggles on and on hes known

A vow unto his own

That never from this day

His will theyll take away

 

What Ive felt

What Ive known

Never shined through in what Ive shown

Never be

Never see

Wont see what might have been

 

What Ive felt

What Ive known

Never shined through in what Ive shown

Never free

Never me

So I dub the unforgiven

 

They dedicate their lives2

To running all of his

He tries to please them all2

This bitter man he is

Throughout his life the same

Hes battled constantly

This fight he cannot win

A tired man they see no longer cares

The old man then prepares

To die regretfully

That old man here is me

 

What Ive felt

What Ive known

Never shined through in what Ive shown2

Never be

Never see

Wont see what might have been

 

What Ive felt

What Ive known

Never shined through in what Ive shown

Never free

Never me

So I dub the unforgiven

 

You labeled me3

Ill label you

So I dub the unforgiven3

222

 

Αρχείο εξόδου:

 

AZ_MAX{I}=16

az_MAX{e}=131

0-9_MAX{2}=6

 

Screenshot to stdout:

 

<a href=t36koib67bco62jeu8d2.jpg' alt='t36koib67

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...