Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
/calc
15 changes: 0 additions & 15 deletions Makefile

This file was deleted.

16 changes: 0 additions & 16 deletions README.md

This file was deleted.

Binary file removed calc
Binary file not shown.
35 changes: 0 additions & 35 deletions calc.c

This file was deleted.

Binary file removed calc.o
Binary file not shown.
20 changes: 20 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// main.c
#include <stdio.h>
#include <string.h>
#include "my_fscanf.h"
int main() {
FILE* fp = fopen("points.txt", "r");
char type[10];
double x, y;
int line;
if (fp != NULL) {
my_fscanf(fp, "%d", &line);
for (int i = 0; i < line; i++) {
my_fscanf(fp, "%s (%f,%f)", type, &x, &y);
if (strcmp(type, "point") == 0) {
printf("X: %f:, Y: %f\n", x, y);
}
}
}
return 0;
}
91 changes: 91 additions & 0 deletions my_fscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// file: my_fscanf.c
#include "my_fscanf.h"
#include <stdarg.h>
#include <ctype.h>
void my_fscanf(FILE *fp, const char *format, ...) {
va_list list;
va_start(list, format);
while (*format) {
if (*format == '%') {
int c;
format++;
switch (*format) {
case 'd':
{
while (isspace(c = getc(fp))) {}

int* addr = va_arg(list, int*);
*addr = 0;
while (isdigit(c)) {
*addr = *addr * 10 + c - '0';
c = getc(fp);
}

while (isspace(c = getc(fp))) {}

format++;
ungetc(c, fp);
break;
}
case 'c':
{
while (isspace(c = getc(fp))) {}

char* addr = va_arg(list, char*);
*addr = c;
format++;
break;
}
case 's':
{
char* addr = va_arg(list, char*);
int cnt = 1;
while (isspace(c = getc(fp))) {}

addr[0] = c;

while (!isspace(c = getc(fp))) {
addr[cnt] = c;
cnt++;
}
addr[cnt] = 0; //add null char
format++;
ungetc(c, fp);
break;
}
case 'f':
{
double* addr = va_arg(list, double*);
double decimalFraction = 0.1;

while (isspace(c = getc(fp))) {}
*addr = 0;
while (isdigit(c)) {
*addr = *addr * 10 + c - '0';
c = getc(fp);
}

if (c == '.') {
c = getc(fp);
while (isdigit(c)) {
*addr = *addr + (c - '0') * decimalFraction;
decimalFraction *= 0.1;
c = getc(fp);
}
}
format++;
ungetc(c, fp);
break;
}

}

}
else {
int c = getc(fp);
if (*format == c)
format++;
}
}
va_end(list);
}
6 changes: 6 additions & 0 deletions my_fscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// file: my_fscanf.h
#ifndef MY_FSCANF_H
#define MY_FSCANF_H
#include <stdio.h>
void my_fscanf(FILE *fp, const char *format, ...);
#endif /* !MY_FSCANF_H */
16 changes: 0 additions & 16 deletions operators.c

This file was deleted.

4 changes: 0 additions & 4 deletions operators.h

This file was deleted.

Binary file removed operators.o
Binary file not shown.
5 changes: 5 additions & 0 deletions points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
point (1.3,2.2)
point (2.5,3.0)
point (3.2,4.4)
point (1.0,5.9)
5 changes: 0 additions & 5 deletions read.txt

This file was deleted.