C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-14-2002, 02:06 PM   #1
Unregistered
Guest
 
Posts: n/a
Question File I/O problems!!! Help!!!

I cannot understand files I/O, I have the pseudo-code for the program and some code I tried, but I cannot ge it. Could you help me please?

*The input file is read character into an array. The array is a large array of 200 characters, and acts as temporary storage before being output after modification to a new file.

*The output file is written starting from the character at the end of the array, and taking characters from the array in reverse order until the start of the array is reached. Counter is used to keep track of where the read/write is currently accessing the array.

* Create an input text file using Windows notepad

#include <stdio.h>



void main()
{
// Initialise array of total 200 for characters
char name[200];

// Initialise single character for each read/write
FILE *f_input;

// Initialise variable for counter to zero
int counter=0

// Open input file
FILE *f_input = fopen("FILE.TXT", "r");


// While not at end of file (loop start)
while (f_input==NULL);

// read single character from input file
fget(f_input);

// Store character into array [counter]

// Increment counter by one
count=count+1
// End of loop code



// Close input file
fclose(f_input);
}


// Decrement counter by one (This moves counter back one position to end of array)
counter=counter-1

// Open output file (different filename to input file)
FILE *f_output

// While counter is greater than zero (loop start)
While (count=>0)

// Get current character at array [counter]

// Write this single character to output file
fput(
// Decrement counter by one
counter=counter-1

// End of loop code

Close output file
fclose(f_output);

// End
}
  Reply With Quote
Old 05-14-2002, 02:07 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
Well you started off wrong:

> void main()

Never use void main.

Quote:
// Initialise single character for each read/write
FILE *f_input;

// Initialise variable for counter to zero
int counter=0

// Open input file
FILE *f_input = fopen("FILE.TXT", "r");
Why are you creating two instances of 'f_input'?

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-14-2002, 02:12 PM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,689
> // While not at end of file (loop start)
while (f_input==NULL);

// read single character from input file
fget(f_input);


The code for reading each line of a file is
Code:
while ( fgets( name, sizeof(name), f_input ) != NULL ) {
    // do something with name
}
Salem is offline   Reply With Quote
Old 05-17-2002, 07:51 PM   #4
Unregistered
Guest
 
Posts: n/a
There is something wrong with the code, it just stops.......right after i have enter path for the file name, say artists.txt

which has the following inside.
India Arie CD 3/27/2001 Motown 15.99 india.jpg Recent newcomer to the music
Jill Scott CD 7/18/2000 Hidden Beach 16.99 jill.jpg Scott is a modern R&B singer from Philadelphia
Maxwell ? Cassette 8/21/2001 Columbia 17.99 Maxwell.jpg soul singer Maxwell (his middle name) had to suffer the ignominy of his record company sitting on his debut album

please help, i am this far and now am stuck.

Code:
/************************************************************/
// Preprocessor Directives
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SUCCESSFUL		1	// Return value
#define FAILED			0	// Return value
#define MAX_STR_SIZE	256	// Max string/filename size
#define MIN_FORMAT		8	// Max minimum format fields
#define MAX_ARTISTS		100	// Max artists allowed in data file
#define FIELDS_COMPLETE	2	// Return value for successful consecutive 
#define THREE_FIELDS	3	// Return value for 3 fields successfully read


/************************************************************/
// Global Declarations

typedef struct
	{
	 char	*cpFormat;
	 char	*cpDate;
	 char	*cpLabel;
	 float	fCost;
	} LABEL;								// Artist label-related info

typedef struct
	{
	 char	*cpFirstName;
	 char	*cpLastName;
	 LABEL	laLabel;
	 char	*cpImageFile;
	 char	*cpBio;							// Artist info
	} ARTIST;

char gcFileName[MAX_STR_SIZE]	= {' '};	// Stores file name
int giArtistQty					= 0;		// Artist quantity in data file


/************************************************************/
// Prototype Declarations
int getFile();
void processFile(ARTIST *);
void renderHTML(ARTIST *);


/************************************************************/
// Main Function
void main(){
	
	// Local Declarations
	ARTIST *arList = NULL;
	
	// Statements
	if(getFile() == SUCCESSFUL){
		arList = (ARTIST *)malloc(sizeof(ARTIST)*giArtistQty);
		processFile(arList);
		renderHTML(arList);
	}// if
}

/************************************************************/
// Function Definitions
/*----------------------------------------
Name	: getFile
Comments: Validates the format of the file
Return	: integer
----------------------------------------*/
int getFile(){

	// Local Declarations
	//char cRead		= ' ';	// Stores char for reading
	char cCheck			= ' ';	// Stores char for checking
	int c				= 0;	// Counter
	FILE *fpDataFile	= NULL;	// FILE structure
	int iDataRead		= 0;	// Counter
	char *cpTemp		= NULL;	// Temp string storage
	char cTemp			= ' ';
	float fTemp			= 0;

	// Statements
	printf("\nEnter the path and file name and press <ENTER> : ");
	scanf("%s", gcFileName);
	fpDataFile = fopen(gcFileName, "r");
	if(fpDataFile == NULL){
		printf("\n\n\aERROR:	File does not exist.");
		return FAILED;
	}
	
	cpTemp = (char *)malloc(sizeof(char)*MAX_STR_SIZE);

	do{
		iDataRead = fscanf(fpDataFile,"%s %s %s %s", cpTemp,cpTemp,cpTemp,cpTemp);
		if(iDataRead == EOF && c == 0){
			printf("\n\n\aERROR:	File is empty.");
			return FAILED;
		}

		iDataRead = fscanf(fpDataFile,"%s %c %f", cpTemp,cTemp,fTemp);
		if(iDataRead != THREE_FIELDS && iDataRead != EOF){
			
			do{
				iDataRead = fscanf(fpDataFile,"%s %c %f", cpTemp,cTemp,fTemp);
			} while(iDataRead != THREE_FIELDS && iDataRead != EOF && cTemp != '\n');
		
			if(cTemp == '\n'){
				printf("\n\n\aERROR:	File contents improperly formatted.");
				return FAILED;
			}
		}
		if(iDataRead != EOF){
			do{
				cCheck = cTemp;
				iDataRead = fscanf(fpDataFile, "%s%c", cpTemp, cTemp);
			}while(cTemp != '\n' && iDataRead != EOF);
			c++;
			if(c >= MAX_ARTISTS){
				printf("\n\n\aERROR:	File cannot contain data for more than %d artists.", MAX_ARTISTS);
				return FAILED;
			}// if
		}// if
	} while(iDataRead != EOF);
	giArtistQty = (cCheck == '\n')?(c-1):c;
	fclose(fpDataFile);
	free(cpTemp);
	return SUCCESSFUL;
}


/*----------------------------------------
Name	: processFile
Comments: Stores data from file into structure
Return	: void
----------------------------------------*/
void processFile(ARTIST *arList){

	// Local Declarations
	char *cpTemp1		= NULL;	// Temp string storage
	char *cpTemp2		= NULL;	// Temp string storage
	FILE *fpDataFile	= NULL;	// FILE pointer
	int a				= 0;	// Counter
	char cChar			= ' ';	// Temp character storage

	// Statements
	cpTemp1 = (char *)malloc(sizeof(char)*MAX_STR_SIZE);
	cpTemp2 =	(char *)malloc(sizeof(char)*MAX_STR_SIZE);
	fpDataFile = fopen(gcFileName, "r");

	for(a=0;a<giArtistQty;a++){
		
		fscanf(fpDataFile, "%s", cpTemp1);
		(arList+a)->cpFirstName = (char 
*)malloc(sizeof(char)*((strlen(cpTemp1))+1));
		*( (arList+a)->cpFirstName ) = '\0';
		strcat( (arList+a)->cpFirstName, cpTemp1);
		
		fscanf(fpDataFile, "%s", cpTemp1);
		(arList+a)->cpLastName = (char 
*)malloc(sizeof(char)*((strlen(cpTemp1))+1));
		*( (arList+a)->cpLastName ) = '\0';
		if(*cpTemp1 != '?'){
			strcat( (arList+a)->cpLastName, cpTemp1 );
		}	
		
		fscanf(fpDataFile, "%s", cpTemp1);
		(arList+a)->laLabel.cpFormat = (char 
*)malloc(sizeof(char)*((strlen(cpTemp1))+1));
		*( (arList+a)->laLabel.cpFormat ) = '\0';
		strcat( (arList+a)->laLabel.cpFormat, cpTemp1);
		
		fscanf(fpDataFile, "%s", cpTemp1);
		(arList+a)->laLabel.cpDate = (char 
*)malloc(sizeof(char)*((strlen(cpTemp1))+1));
		*( (arList+a)->laLabel.cpDate ) = '\0';
		strcat( (arList+a)->laLabel.cpDate, cpTemp1);

		*cpTemp2 = '\0';
		if(fscanf(fpDataFile, "%s %f", cpTemp1, &((arList+a)->laLabel.fCost)) 
!= FIELDS_COMPLETE){
			do{
				strcat(cpTemp2, cpTemp1);
				strcat(cpTemp2, " ");
			}while(fscanf(fpDataFile, "%s %f", cpTemp1, 
&((arList+a)->laLabel.fCost)) != FIELDS_COMPLETE);
		}// if
		strcat(cpTemp2, cpTemp1);
		(arList+a)->laLabel.cpLabel = (char 
*)malloc(sizeof(char)*((strlen(cpTemp2))+1));
		*( (arList+a)->laLabel.cpLabel ) = '\0';
		strcat( (arList+a)->laLabel.cpLabel, cpTemp2 );
		
		fscanf(fpDataFile, "%s", cpTemp1);
		(arList+a)->cpImageFile = (char 
*)malloc(sizeof(char)*((strlen(cpTemp1))+1));
		*( (arList+a)->cpImageFile ) = '\0';
		strcat( (arList+a)->cpImageFile, cpTemp1 );

		*cpTemp2 = '\0';
		if(fscanf(fpDataFile, "%s%c", cpTemp1, &cChar) == FIELDS_COMPLETE && 
cChar != '\n'){
			do{
				strcat(cpTemp2, cpTemp1);
				strcat(cpTemp2, " ");
			}while(fscanf(fpDataFile, "%s%c", cpTemp1, &cChar) != EOF && cChar 
!= '\n');
		}
		strcat(cpTemp2, cpTemp1);
		(arList+a)->cpBio = (char 
*)malloc(sizeof(char)*((strlen(cpTemp2))+1));
		*((arList+a)->cpBio) = '\0';
		strcat( (arList+a)->cpBio, cpTemp2 );
		
	}// for

	fclose(fpDataFile);
	free(cpTemp2);
	free(cpTemp1);

}



/*----------------------------------------
Name	: renderHTML
Comments: Creates HTML pages from artist data
Return	: void
----------------------------------------*/
void renderHTML(ARTIST *arList){

	// Local Declarations
	int	a				= 0;	// Counter
	FILE *fpHTML		= NULL;	// FILE pointer
	FILE *fpIndex		= NULL;	// FILE pointer
	char *cpFileName	= NULL;	// HTML file name

	// Statements
	cpFileName = (char *)malloc(sizeof(char)*MAX_STR_SIZE);
	fpIndex = fopen("index.html", "w");
	fprintf(fpIndex,"<html>\n<head>\n<title>Music Artists</title>\n</head>\n<body>\n");
	fprintf(fpIndex,"<h1 align=\"center\"><u>Music Artists</u></h1>\n");
	fprintf(fpIndex,"<p align=\"left\">Make your selection from the list below:</p>\n<ul>\n");

	for(a = 0;a < giArtistQty;a++){
		
		*cpFileName = '\0';
		strcat(cpFileName, (arList+a)->cpFirstName);
		strcat(cpFileName, "_");
		strcat(cpFileName, (arList+a)->cpLastName);
		strcat(cpFileName, ".html");
		fpHTML = fopen(cpFileName, "w");
		fprintf(fpHTML,"<html>\n<head>\n<title>%s %s</title>\n</head>\n<body>\n",(arList+a)->cpFirstName,(arList+a)->cpLastName);
		fprintf(fpHTML,"<img SRC=\"%s\" BORDER=0 height=170 width=170>\n",(arList+a)->cpImageFile);
		free((arList+a)->cpImageFile);
		fprintf(fpHTML,"<br><b>Name</b>: %s %s\n",(arList+a)->cpFirstName,(arList+a)->cpLastName);
		fprintf(fpHTML,"<br><b>Format</b>: %s\n", 
(arList+a)->laLabel.cpFormat);
		free((arList+a)->laLabel.cpFormat);
		fprintf(fpHTML,"<br><b>Release Date</b>: %s\n",(arList+a)->laLabel.cpDate);
		free((arList+a)->laLabel.cpDate);
		fprintf(fpHTML,"<br><b>Label</b>: %s\n",(arList+a)->laLabel.cpLabel);
		free((arList+a)->laLabel.cpLabel);
		fprintf(fpHTML,"<br><b>Cost</b>: $%.2f\n",(arList+a)->laLabel.fCost);
		fprintf(fpHTML,"<br><b>Biography</b>: %s\n<br>&nbsp;\n</body>\n</html>",(arList+a)->cpBio);
		free((arList+a)->cpBio);
		fclose(fpHTML);
		fprintf(fpIndex,"  <li>\n  <p align=\"left\"><a href=\"%s\">%s %s</a></li>\n",cpFileName,(arList+a)->cpFirstName,(arList+a)->cpLastName);
		free((arList+a)->cpFirstName);
		free((arList+a)->cpLastName);
		
	}

	free(arList);
	fprintf(fpIndex,"</ul>\n<br>&nbsp;\n</body>\n</html>");
	fclose(fpIndex);
	free(cpFileName);

}
  Reply With Quote
Old 05-17-2002, 08:09 PM   #5
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>iDataRead = fscanf(fpDataFile,"%s %c %f", cpTemp,cTemp,fTemp);
For future reference, the scanf family requires pointers be passed as arguments. Just the name is okay for arrays, but for regular variables you need to pass the address:
Code:
iDataRead = fscanf(fpDataFile,"%s %c %f", cpTemp,&cTemp,&fTemp);
This was giving me an access violation and is probably what your problem is. Let me know if you have any more problems after changing your calls to scanf.

-Prelude
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File transfer- the file sometimes not full transferred shu_fei86 C# Programming 13 03-13-2009 12:44 PM
Need Help Fixing My C Program. Deals with File I/O Matus C Programming 7 04-29-2008 07:51 PM
help with text input Alphawaves C Programming 8 04-08-2007 04:54 PM
Simple File encryption caroundw5h C Programming 2 10-13-2004 10:51 PM
Need a suggestion on a school project.. Screwz Luse C Programming 5 11-27-2001 02:58 AM


All times are GMT -6. The time now is 02:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22