M
Martin Joergensen
Hi,
Okay I'm really bad at this... I tried to make a program that stores some
characters in a large 1-D array and some of this information for instance
the first 8 lines of text should then be written both to a file and to the
screen....
I get 6 errors + 8 warnings...
- - - --
/* purpose of program:
Instead of making two identically printf(" ") + fprinft(fp, " ");
statements, a pointer to char
holds the text that should be output to *both* the screen and to a file.
//printf("\n");
// printf("********************** INPUT AND RESULTS:
*************************\n");
// printf("* X_A * X_B * Y_A * Y_B *** r(test) * r(degrees) *\n");
//
printf("***********************************************************************\n");
// printf("* %7.2lf * %7.2lf * %7.2lf * %7.2lf *** %7.2lf * %7.2lf *\n",
X_A, X_B, Y_A, Y_B, r_test, r_degrees);
//
printf("***********************************************************************\n");
*/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for memcpy
void repeat(char *dest_array, char ch, int n, unsigned int *position);
void insert(char *dest_array, char *source_str, unsigned int *position);
//////////////
main()
{
char *text;
char *str[80]; /* static mem. allocated, max 1 line = 80 chars */
FILE *outputfile;
unsigned long size = 1600*sizeof(char); /* space for 20 lines */
unsigned int *position = 0;
text = malloc(size);
if (!text)
{
fprintf(stderr, "%s: line %d, malloc(%lu) failed.\n", __FILE__, __LINE__,
size);
exit(EXIT_FAILURE);
}
memcpy(text, "\n", (size_t) 2 ); /* start by moving to the next line */
insert(text, str, position); /* insert string into text-array (this array is
the one to be written to both screen+file) */
repeat(text, '*', 20, position); /* make 20 stars: ****************** etc...
*/
memcpy(text, " INPUT ", (size_t) 7 ); /* move to text-array */
insert(text, str, position);
if ( (outputfile=fopen("result.txt", "w") )==NULL)
{
printf("\nError file: \"result.txt\".\n\n");
}
else
{
/* write data from text both to screen *and* to file - still unsure about
how to do this part too */
}
fclose(outputfile);
system("PAUSE");
return 0;
}
/* functions */
void repeat(char *dest_array, char ch, int n, unsigned int *position)
{
int i;
for(i=0; i<n; i++)
memcpy(dest_array[position+i], ch, (size_t) 1);
*position += n;
}
void insert(char *dest_array, char *source_str, unsigned int *position)
{
int i, n;
n = strlen(source_str);
while(i=0; i<n; i++)
*(dest_array[position+i]) = *source_str;
*position += n;
}
- - - - -
Suggestions? I need a little help to get going, I think
Sorry, I hate to ask again...
Best regards / Med venlig hilsen
Martin Jørgensen
Okay I'm really bad at this... I tried to make a program that stores some
characters in a large 1-D array and some of this information for instance
the first 8 lines of text should then be written both to a file and to the
screen....
I get 6 errors + 8 warnings...
- - - --
/* purpose of program:
Instead of making two identically printf(" ") + fprinft(fp, " ");
statements, a pointer to char
holds the text that should be output to *both* the screen and to a file.
//printf("\n");
// printf("********************** INPUT AND RESULTS:
*************************\n");
// printf("* X_A * X_B * Y_A * Y_B *** r(test) * r(degrees) *\n");
//
printf("***********************************************************************\n");
// printf("* %7.2lf * %7.2lf * %7.2lf * %7.2lf *** %7.2lf * %7.2lf *\n",
X_A, X_B, Y_A, Y_B, r_test, r_degrees);
//
printf("***********************************************************************\n");
*/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for memcpy
void repeat(char *dest_array, char ch, int n, unsigned int *position);
void insert(char *dest_array, char *source_str, unsigned int *position);
//////////////
main()
{
char *text;
char *str[80]; /* static mem. allocated, max 1 line = 80 chars */
FILE *outputfile;
unsigned long size = 1600*sizeof(char); /* space for 20 lines */
unsigned int *position = 0;
text = malloc(size);
if (!text)
{
fprintf(stderr, "%s: line %d, malloc(%lu) failed.\n", __FILE__, __LINE__,
size);
exit(EXIT_FAILURE);
}
memcpy(text, "\n", (size_t) 2 ); /* start by moving to the next line */
insert(text, str, position); /* insert string into text-array (this array is
the one to be written to both screen+file) */
repeat(text, '*', 20, position); /* make 20 stars: ****************** etc...
*/
memcpy(text, " INPUT ", (size_t) 7 ); /* move to text-array */
insert(text, str, position);
if ( (outputfile=fopen("result.txt", "w") )==NULL)
{
printf("\nError file: \"result.txt\".\n\n");
}
else
{
/* write data from text both to screen *and* to file - still unsure about
how to do this part too */
}
fclose(outputfile);
system("PAUSE");
return 0;
}
/* functions */
void repeat(char *dest_array, char ch, int n, unsigned int *position)
{
int i;
for(i=0; i<n; i++)
memcpy(dest_array[position+i], ch, (size_t) 1);
*position += n;
}
void insert(char *dest_array, char *source_str, unsigned int *position)
{
int i, n;
n = strlen(source_str);
while(i=0; i<n; i++)
*(dest_array[position+i]) = *source_str;
*position += n;
}
- - - - -
Suggestions? I need a little help to get going, I think
Sorry, I hate to ask again...
Best regards / Med venlig hilsen
Martin Jørgensen