How to do things AI Noob vs. Pro
List biggest files Free Open Source: Swiss File Knifea command line
Depeche View
command line
free external tools,
cpp sources
articles |
/* C array source code example: - read text file into a variable sized array - with a variable size per text line (up to a limit) - sort the array - write array content into another text file. */ #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> // tell that these functions exist further below: int perr(const char *pszFormat, ...); int processData(char **apText, int iLinesUsed, int iLinesAlloced); // maximum supported characters per line: #define MAX_LINE_LENGTH 100000 // declare one global buffer to store a huge line: char szLineBuf[MAX_LINE_LENGTH+100]; // +100 is safety space, to avoid crash on off-by-one errors. int main(int argc, char *argv[]) { if (argc < 3) return 9+perr("specify input and output filename.\n"); char *pszInFile = argv[1]; char *pszOutFile = argv[2]; // preparation: make a clean empty buffer memset(szLineBuf, 0, sizeof(szLineBuf)); // full flexible array for text storage: int iLinesAlloced = 10; // initial array size int iLinesUsed = 0; // number of lines actually used char **apTextData = malloc(sizeof(char*) * (iLinesAlloced+10)); // in C++, it's: = new char*[iLinesAlloced+10]; if (!apTextData) return 9+perr("out of memory\n"); // read input file into the array. use binary mode "rb" // to avoid CR/LF line end conversions. FILE *fin = fopen(pszInFile , "rb"); if (!fin ) return 9+perr("cannot read %s\n" , pszInFile); // while not end of file, read another line while (fgets(szLineBuf, sizeof(szLineBuf)-10, fin) != 0) { // space left in the array? if (iLinesUsed >= iLinesAlloced) { // no: expand the array int iNewSize = iLinesAlloced * 2 + 10; printf("[expanding array to %d lines]\n", iNewSize); char **apNewData = malloc(sizeof(char*) * (iNewSize+10)); // in C++: = new char*[iNewSize+10]; if (!apNewData) { perr("out of memory\n"); break; } memcpy(apNewData, apTextData, sizeof(char*) * iLinesUsed); free(apTextData); // in C++: delete [] apTextData; apTextData = apNewData; iLinesAlloced = iNewSize; } // strip CR/LF from line endings so we get pure text char *psz = strchr(szLineBuf, '\r'); if (psz) *psz = '\0'; psz = strchr(szLineBuf, '\n'); if (psz) *psz = '\0'; // store the line apTextData[iLinesUsed] = strdup(szLineBuf); iLinesUsed++; } fclose(fin); // do some processing on the array contents processData(apTextData, iLinesUsed, iLinesAlloced); // write the whole array to output file. use text mode "w" // so under windows it will create CRLF line endings. FILE *fout = fopen(pszOutFile, "w"); if (!fout) return 9+perr("cannot write %s\n", pszOutFile); int iLine; for (iLine=0; iLine<iLinesUsed; iLine++) { strncpy(szLineBuf, apTextData[iLine], MAX_LINE_LENGTH); szLineBuf[MAX_LINE_LENGTH-2] = '\0'; // safety w/ space for LF strcat(szLineBuf, "\n"); // produces CRLF under windows int nlen = strlen(szLineBuf); if (fwrite(szLineBuf, 1, nlen, fout) != nlen) return 9+perr("failed to fully write %s\n", pszOutFile); } fclose(fout); // cleanup free(apTextData); // in C++: delete [] apTextData; return 0; } // example 1: sort the array text lines alphabetically // with a very simple sorting algorithm (bubblesort). int processData(char **apText, int iLinesUsed, int iLinesAlloced) { int iOuterIdx, iInnerIdx; for (iOuterIdx=0; iOuterIdx<iLinesUsed; iOuterIdx++) { for (iInnerIdx=iOuterIdx+1; iInnerIdx<iLinesUsed; iInnerIdx++) { char *pszLine1 = apText[iOuterIdx]; char *pszLine2 = apText[iInnerIdx]; if (strcmp(pszLine1, pszLine2) > 0) { // swap both lines apText[iOuterIdx] = pszLine2; apText[iInnerIdx] = pszLine1; } } // show a progress indicator with percent info printf("[sorting ... %02d%%] \r", iOuterIdx * 100 / (iLinesUsed ? iLinesUsed : 1)); } printf("sorted %d lines.\n", iLinesUsed); return 0; } // helper function: print error with variable parameters. int perr(const char *pszFormat, ...) { va_list argList; va_start(argList, pszFormat); char szBuf[1024]; vsprintf(szBuf, pszFormat, argList); fprintf(stderr, "error: %s", szBuf); return 0; }
// See also:
Simple C example for a fixed-size array
.
|