int main(int argc, char *argv[]) { FILE *testfile; long posn; if (argc < 3) usage(); testfile = cant(argv[2], "r+b"); switch (toupper(argv[1][0])) { case 'C': posn = atol(argv[3]); printf("fChSize(%s, %ld) returned %d\n", argv[1], posn, fChSize(testfile, posn)); break; case 'S': printf("fStub(%s) returned %d\n", argv[1], fStub(testfile)); break; default: usage(); } fclose(testfile); return EXIT_SUCCESS; }
void b_write(void) { register FILE *fp; register unsigned u; fp = cant("NUL", "wb"); for (u=0; u<500*CHK; ++u) fwrite("Now is the time for all good men to come\n", 41, 1, fp); fclose(fp); }
void b_printf(void) { register FILE *fp; register unsigned u; fp = cant("NUL", "wb"); for (u=0; u<50*CHK; ++u) fprintf(fp, "Now is %d time for %d little indians\n", 123, -456); fclose(fp); }
void tu_write(void) { register FILE *fp; register unsigned u; fp = cant("NUL", "wt"); setbuf(fp, NULL); for (u=0; u<100*CHK; ++u) fwrite("Now is the time for all good men to come\n", 41, 1, fp); fclose(fp); }
void create_text_file(void) { FILE *out; out = cant("test.txt", "w"); fputs("This is a test!\n", out); fputs("This is a dummy line to delete...\n", out); fputs("This is a dummy line to modify...\n", out); fputs("All done!\n", out); fclose(out); show_text_file("The file as written is"); }
main(int argc, char *argv[]) { FILE *infile = stdin, *outfile = stdout; char line[2][256]; /* Nice & roomy */ if (Error_ == getopts(argc, argv)) usage(-1); if (help) usage(0); if (1 < xargc) infile = cant(xargv[1], "r"); if (2 < xargc) outfile = cant(xargv[2], "w"); while (NULL != fgets(line[0], 255, infile)) { char *p1, *p2; int OK; strcpy(line[1], line[0]); if ('\n' == LAST_CHAR(line[1])) LAST_CHAR(line[1]) = NUL; if (fold) strupr(line[1]); if (punc) rmpunc(line[1]); for (p1 = line[1], p2 = &LAST_CHAR(line[1]), OK = 1; p2 > p1; ++p1, --p2) { if (*p1 != *p2) { OK = 0; break; } } if (OK) fputs(line[0], outfile); } return 0; }
main() { FILE *ndx, *desc; char line1[81], line2[81], *ptr; int i; ndx = cant("snippets.ndx", "r"); desc = cant("descript.ion", "w"); while (!feof(ndx)) { if (NULL != (fgets(line1, 81, ndx))) { if ('|' == *line1 || 3 > strlen(line1) || isspace(line1[2]) || '-' == *line1) { continue; } for (ptr = line1 + 2; ' ' != *ptr; ++ptr) fputc(*ptr, desc); fputs(" <", desc); if (isspace(line1[19])) { for (i = 15; i < 18; ++i) fputc(line1[i], desc); fprintf(desc, ">%s", line1 + 19); } else { for (i = 15; i < 18; ++i) fputc(line2[i], desc); fprintf(desc, ">%s", line2 + 19); } strcpy(line2, line1); } } return EXIT_SUCCESS; }
void show_text_file(char *txt) { FILE *in; char line[80]; in = cant("test.txt", "r"); printf("\n%s:\n\n", txt); while (!feof(in)) { if (NULL != fgets(line, 80, in)) fputs(line, stdout); } }
main(int argc, char *argv[]) { int i, ch, hist = 0; long n = 0L; double mean = 0., stdev = 0., ftmp; static unsigned bins[256]; FILE *infile; infile = cant(argv[1], "rb"); while (!feof(infile)) { if (EOF == (ch = fgetc(infile))) break; bins[ch] += 1; ++n; } fclose(infile); for (i = 0; i < 256; ++i) { mean += (double)(bins[i]); if (bins[i]) ++hist; } mean /= 256.; for (i = 0; i < 256; ++i) { ftmp = (double)(bins[i]) - mean; stdev += (ftmp * ftmp); } ftmp = stdev / 255.; stdev = sqrt(ftmp); printf("%ld Characters were read from %s\n" "There are an average of %f occurrences of each character\n" "%d Characters out of 256 possible were used\n" "The standard deviation is %f\n" "The coefficient of variation is %f%%\n", n, argv[1], mean, hist, stdev, (100. * stdev) / mean); return EXIT_SUCCESS; }
main() { FILE *in, *out; char line[80], *tmp, *ptr; /* ** Open the original file for reading and a temporary file for writing */ create_text_file(); in = cant("test.txt", "r"); tmp = tmpnam(NULL); out = cant(tmp, "w"); /* ** Read the first line and copy it */ fgets(line, 80, in); fputs(line, out); /* ** Discard the second line */ fgets(line, 80, in); /* ** Add a new line */ fputs("(Isn't it?)\n", out); /* ** Read the 3rd line, modify it, then write it out */ fgets(line, 80, in); ptr = strrchr(line, 'm'); strcpy(ptr, "edit...\n"); fputs(line, out); /* ** Read the last line and copy it */ fgets(line, 80, in); fputs(line, out); /* ** Close the files, delete the old, rename the temp */ fclose(in); fclose(out); remove("test.txt"); rename(tmp, "test.txt"); /* ** Now let's see the results */ show_text_file("The file as modified is"); return 0; }
main() { char Line[INI_LINESIZE]; int Int; long Long; double Double; Boolean_T Bool; struct CfgStruct this_var; FILE* tst; long prices[2]; /* ** First work with a test file */ tst = cant("test.ini", "w"); fputs("[Section 1]\n", tst); fputs("[Section 2]\n", tst); fputs("[Section 3]\n", tst); fputs("[Section 4]\n", tst); fputs("[Section 5]\n", tst); fputs("[Section 6]\n", tst); fclose(tst); puts("Updating the test configuration file"); puts("Updating section 1"); UpdateCfg("test.ini", "Section 1", "string #1", "section 1 test"); puts("Updating section 2"); UpdateCfg("test.ini", "Section 2", "short #2", "2"); puts("Updating section 3"); UpdateCfg("test.ini", "Section 3", "long #3", "3"); UpdateCfg("test.ini", "Section 4", "double #4", "4.4"); UpdateCfg("test.ini", "Section 5", "boolean #5", "Y"); UpdateCfg("test.ini", "Section 6", "boolean #6", "N"); UpdateCfg("test.ini", "", "global string", "\"Hello, world!\" ;Comment"); puts("I've finished the updates, now to try to get the data back"); this_var.Name = "global string"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(0) returned %d; Line=\n", ReadCfg("test.ini", "", &this_var)); puts(Line); this_var.Name = "string #1"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(1) returned %d; Line=\n", ReadCfg("test.ini", "Section 1", &this_var)); puts(Line); this_var.Name = "short #2"; this_var.DataPtr = ∬ this_var.VarType = Cfg_Short; printf("ReadCfg(2) returned %d; Value= ", ReadCfg("test.ini", "Section 2", &this_var)); printf("%d\n", Int); this_var.Name = "long #3"; this_var.DataPtr = &Long; this_var.VarType = Cfg_Long; printf("ReadCfg(3) returned %d; Value = ", ReadCfg("test.ini", "Section 3", &this_var)); printf("%ld\n", Long); this_var.Name = "double #4"; this_var.DataPtr = &Double; this_var.VarType = Cfg_Double; printf("ReadCfg(4) returned %d; Value = ", ReadCfg("test.ini", "Section 4", &this_var)); printf("%f\n", Double); this_var.Name = "boolean #5"; this_var.DataPtr = &Bool; this_var.VarType = Cfg_Boolean; printf("ReadCfg(5) returned %d; Value = ", ReadCfg("test.ini", "Section 5", &this_var)); printf("%c\n", Bool ? 'T' : 'F'); this_var.Name = "boolean #6"; this_var.DataPtr = &Bool; this_var.VarType = Cfg_Boolean; printf("ReadCfg(6) returned %d; Value = ", ReadCfg("test.ini", "Section 6", &this_var)); printf("%c\n", Bool ? 'T' : 'F'); /* ** Look for non-existant sections and/or variables */ Line[0] = NUL; this_var.Name = "string #99"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(99) returned %d; Line=\n", ReadCfg("test.ini", "Section 99", &this_var)); puts(Line); Line[0] = NUL; this_var.Name = "string #99"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(0/99) returned %d; Line=\n", ReadCfg("test.ini", "", &this_var)); puts(Line); Line[0] = NUL; this_var.Name = "string #99"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(1/99) returned %d; Line=\n", ReadCfg("test.ini", "Section 1", &this_var)); puts(Line); Line[0] = NUL; this_var.Name = "string #1"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(1/100) returned %d; Line=\n", ReadCfg("test.ini", "Section 100", &this_var)); puts(Line); /* ** Next, add a section and new variables */ UpdateCfg("test.ini", "", "new global variable", "abc"); UpdateCfg("test.ini", "Section -1", "new variable", "xyz"); /* ** Next work with a sample real PRICES.INI file */ this_var.Name = "Price of #1"; this_var.DataPtr = &prices[0]; this_var.VarType = Cfg_Long; printf("ReadCfg(1) returned %d; Value = ", ReadCfg(PINI_fname, "", &this_var)); printf("%ld\n", prices[0]); this_var.Name = "Price of #2"; this_var.DataPtr = &prices[1]; this_var.VarType = Cfg_Long; printf("ReadCfg(2) returned %d; Value = ", ReadCfg(PINI_fname, "", &this_var)); printf("%ld\n", prices[1]); UpdateCfg("prices.ini", "", "Price of #2", "999"); this_var.Name = "Price of #2"; this_var.DataPtr = &prices[1]; this_var.VarType = Cfg_Long; printf("ReadCfg(2) returned %d; Value = ", ReadCfg(PINI_fname, "", &this_var)); printf("%ld\n", prices[1]); UpdateCfg(PINI_fname, "", "Price of #2", "389"); this_var.Name = "Price of #2"; this_var.DataPtr = &prices[1]; this_var.VarType = Cfg_Long; printf("ReadCfg(2) returned %d; Value = ", ReadCfg(PINI_fname, "", &this_var)); printf("%ld\n", prices[1]); /* ** Finally, try an invalid file name */ this_var.Name = "global string"; this_var.DataPtr = Line; this_var.VarType = Cfg_String; printf("ReadCfg(0) returned %d; Line=\n", ReadCfg("none.ini", "", &this_var)); puts(Line); return EXIT_SUCCESS; }
int UpdateCfg(const char *FileName, char *SectionName, char *VarWanted, char *NewData) { FILE *CfgFile; char line[BUFFERSIZE]; char SectionWanted[BUFFERSIZE]; char CurrentSection[BUFFERSIZE]; enum LineTypes linetype; char var[BUFFERSIZE]; char data[BUFFERSIZE]; char TempFileName[FILENAME_MAX]; FILE *NewCfgFile; int Error = 0; int updated = 0; CfgFile = cant((char *)FileName, "r"); strcpy(TempFileName, FileName); chgext(TempFileName, NULL, "tmp"); NewCfgFile = cant(TempFileName, "w"); strcpy(CurrentSection, "[]"); sprintf(SectionWanted, "[%s]", SectionName); while (EOF != ReadLine(CfgFile, line)) { linetype = SectionLine(line, SectionWanted, CurrentSection); switch (linetype) { case InSection: /* In our section, parse it. */ ParseLine(line, var, data); if ((StrEq(var, VarWanted)) && (!updated)) { strncpy(data, NewData, BUFFERSIZE); data[BUFFERSIZE-1] = NUL; updated = 1; } fprintf(NewCfgFile, "%s = %s\n", var, data); break; case EmptyLine: /* Fall Through. Just copy it. */ case CommentLine: /* Fall Through. Just copy it. */ case NotInSection: /* Fall Through. Just copy it. */ case NewSection: /* Fall Through. Just copy it. */ case FoundSection: /* Fall Through. Just copy it. */ fprintf(NewCfgFile, "%s\n", line); break; case LeavingSection: /* Leaving section, may have to add it */ if (!updated) /* Variable wasn't found, we have */ { /* to add it. */ fprintf(NewCfgFile, "%s = %s\n", VarWanted, NewData); updated = 1; } /* ** Now print current line */ fprintf(NewCfgFile, "%s\n", line); break; } } /* ** Our section may not have even been there, in which case we have ** to add both the variable and the section itself. */ if (!updated) { /* We may have hit EOF while still in our section. */ /* If so, we don't need to add the section header. */ if (!StrEq(CurrentSection, SectionWanted)) fprintf(NewCfgFile, "%s\n", SectionWanted); fprintf(NewCfgFile, "%s = %s\n", VarWanted, NewData); } if (ferror(CfgFile)) Error = -1; if (ferror(NewCfgFile)) Error = -1; fclose(CfgFile); fclose(NewCfgFile); if (!Error) { if (remove(FileName)) return -1; if (rename(TempFileName, FileName)) return -1; } return Error; }
int ReadCfg(const char *FileName, char *SectionName, struct CfgStruct *MyVars) { FILE *CfgFile; char line[BUFFERSIZE]; char SectionWanted[BUFFERSIZE]; char CurrentSection[BUFFERSIZE]; enum LineTypes linetype; char var[BUFFERSIZE]; char data[BUFFERSIZE], *dp; int retval = 0; struct CfgStruct *mv; CfgFile = cant((char *)FileName, "r"); strcpy(CurrentSection, "[]"); sprintf(SectionWanted, "[%s]", SectionName); while (EOF != ReadLine(CfgFile, line)) { linetype = SectionLine(line, SectionWanted, CurrentSection); switch (linetype) { case EmptyLine: break; /* Nothing to parse */ case CommentLine: break; /* Nothing to parse */ case InSection: /* In our section, parse it. */ { ParseLine(line, var, data); for (mv = MyVars; mv->Name; ++mv) { if (StrEq(mv->Name, var)) { switch (mv->VarType) { case Cfg_String: if ('\"' == *data) { dp = data + 1; data[strlen(data)-1] = NUL; } else dp = data; /* ** Use sprintf to assure embedded ** escape sequences are handled. */ sprintf(mv->DataPtr, dp); ++retval; break; case Cfg_Byte: *((unsigned char*)mv->DataPtr) = (unsigned char)atoi(data); ++retval; break; case Cfg_Ushort: *((unsigned int*)mv->DataPtr) = (unsigned int)atoi(data); ++retval; break; case Cfg_Short: *((int*)mv->DataPtr) = atoi(data); ++retval; break; case Cfg_Ulong: *((unsigned long*)mv->DataPtr) = (unsigned long)atol(data); ++retval; break; case Cfg_Long: *((long*)mv->DataPtr) = atol(data); ++retval; break; case Cfg_Double: *((double*)mv->DataPtr) = atof(data); ++retval; break; case Cfg_Boolean: *((int*)mv->DataPtr) = 0; data[0] = tolower(data[0]); if (('y' == data[0]) || ('t' == data[0])) *((int*)mv->DataPtr) = 1; ++retval; break; case Cfg_I_Array: { int *ip; char *str; ip = ((int*)mv->DataPtr); str = strtok(data, " ,\t"); while (NULL != str) { *ip = atoi(str); ip++; str = strtok(NULL, " ,\t"); } ++retval; break; } default: #ifdef TEST printf("Unknown conversion type\n"); #endif retval = -1; break; } } if (-1 == retval) break; }; /* ** Variable wasn't found. If we don't want it, ** then ignore it */ } case NotInSection: break; /* Not interested in this line */ case NewSection: break; /* Who cares? It's not our section */ case FoundSection: break; /* We found our section! */ case LeavingSection: break; /* We finished our section! */ } if (-1 == retval) break; } if (ferror(CfgFile)) { fclose(CfgFile); return -2; } else { fclose(CfgFile); return retval; } }