In: Computer Science
Use the given Strings.c and Strings.h module:
Strings.c:
#include "Strings.h"
#include <string.h>
#include<stdlib.h>
#include<stdio.h>
char* substring(char* str, int iPos){
if(iPos > strlen(str)||iPos < 0)return (char*)NULL;
char* substr;
substr = &str[iPos];
return substr;
}
int charPosition(char* str, char c){
char* string = (char*)malloc(strlen(str)+1);
int i;
for(i = 0; i < strlen(str)+1; i++)
{
if(str[i] == c)
{
return i;
}
}
return -1;
}
Strings.h:
#include <string.h>
/* substring - return a pointer to the substring beginning at
the iPos-th position.
* If iPos is out-of-range, return (char*)NULL
*/
char* substring(char* str, int iPos);
char* substringDisplay(char* str, int iPos);
/* charPosition - return the position of the first occurance of
c in str.
* If c not present, return -1
*/
int charPosition(char* str, char c);
Write a function
char* fgetString(FILE* pFIn);
that reads the string length from an open file, allocates the memory and reads the string into it.
Write a test driver program that opens a file (file name from the command line arguments) and reads all strings from a file and writes them, one string per line, to standard out
1.Write a function
char* fgetString(FILE* pFIn);
that reads the string length from an open file, allocates the memory and reads the string into it.
2.Write a test driver program that opens a file (file name from the command line arguments) and reads all strings from a file and writes them, one string per line, to standard output
***********************
1.
#include <stdio.h>
#include <conio.h>
char* fgetString(FILE*);
int main()
{
FILE *f;
char *str;
f = fopen("testcontrol.txt", "w+");
fprintf(f, "A testing for fprintf...\n");
fputs("A testing for fputs...\n", f);
fclose(f);
char* fgetString(FILE* fp)
{
char buff[255];
fp = fopen("testcontrol.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );
fgets(buff, 255, (FILE*)fp);
printf("2: %s\n", buff );fgets(buff, 255, (FILE*)fp);
printf("3: %s\n", buff );
fclose(fp);
}
return 0;
}
********************
2.
#include<stdio.h>
#include<conio.h>
int main(int argc, char* argv[])
{
FILE *t;
t=fopen(argv[0],"r");
if(t==NULL)
{
printf("Can't open file%s",argv[0]);
printf("\n press any key to exit...");
exit();
}
while(!EOF)
{
fscanf(t,"%s", str);
printf("%s\n", str);
}
fclose(t);
return 0;
}
************************