Reading a text file
-
Is there a way to create a program that would read a text file and every time it found a space it would replace the space with a line return?
sure, It isn't a difficult thing:-) read the text to buffer, and chr the space char ' ', then replace it with '\r', OK, output it to file or screen. Best Regards
-
sure, It isn't a difficult thing:-) read the text to buffer, and chr the space char ' ', then replace it with '\r', OK, output it to file or screen. Best Regards
Is it still possible with the console app? I would like to output it to a new file.
-
Is it still possible with the console app? I would like to output it to a new file.
Andrew Admire wrote: Is it still possible with the console app? Of course. Andrew Admire wrote: I would like to output it to a new file. Not a problem. Is this an MFC-based application?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Andrew Admire wrote: Is it still possible with the console app? Of course. Andrew Admire wrote: I would like to output it to a new file. Not a problem. Is this an MFC-based application?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
I do not know how to use MFC so no, I would like it to be straight console app based.
-
I do not know how to use MFC so no, I would like it to be straight console app based.
Andrew Admire wrote: ...I would like it to be straight console app based. MFC can be used in both GUI and console applications.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Andrew Admire wrote: ...I would like it to be straight console app based. MFC can be used in both GUI and console applications.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Ok, then no I would like to use C++.
-
Ok, then no I would like to use C++.
This should get you close. You can make the necessary changes and organize it a bit better.
FILE *pFile = fopen("C:\\!conMania Collection\\list71.txt", "rb");
if (NULL != pFile)
{
// how big is the file?
fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);// allocate a buffer for it char \*pBuffer = new char\[lSize\]; // read the entire file // much more efficient than reading one byte at a time fread(pBuffer, sizeof(char), lSize, pFile); fclose(pFile); // change the spaces to linefeeds for (int x = 0; x < lSize; x++) { if (pBuffer\[x\] == ' ') pBuffer\[x\] = '\\n'; } // create a new file pFile = fopen("C:\\\\!conMania Collection\\\\list71\_temp.txt", "wb"); if (NULL != pFile) { fwrite(pBuffer, sizeof(char), lSize, pFile); fclose(pFile); } delete \[\] pBuffer;
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Is there a way to create a program that would read a text file and every time it found a space it would replace the space with a line return?
this needs a lot of polish, but it does the bare essentials. in_filename and out_filename could be selected using CFileDialog. they are hard coded here. #include windows.h #include string.h #include stdio.h main() { FILE *infile_p; // pointer for read file FILE *outfile_p; // pointer for write file int byte;// var for byte read from file if ((infile_p = fopen("in_filename", "r"))==NULL) // open file for read { exit(0); } if ((outfile_p = fopen("out_filename", "w"))==NULL) // open file for write { exit(0); } while (!feof(infile_p)) // end of file test { byte = (fgetc(infile_p)); if (byte == 32) // look for a space { fprintf(outfile_p ,"%c%c", 0x0d, 0x0a);/ replace it with CR/LF } else { fprintf( outfile_p, "%c", byte); } } return(0); }
-
This should get you close. You can make the necessary changes and organize it a bit better.
FILE *pFile = fopen("C:\\!conMania Collection\\list71.txt", "rb");
if (NULL != pFile)
{
// how big is the file?
fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);// allocate a buffer for it char \*pBuffer = new char\[lSize\]; // read the entire file // much more efficient than reading one byte at a time fread(pBuffer, sizeof(char), lSize, pFile); fclose(pFile); // change the spaces to linefeeds for (int x = 0; x < lSize; x++) { if (pBuffer\[x\] == ' ') pBuffer\[x\] = '\\n'; } // create a new file pFile = fopen("C:\\\\!conMania Collection\\\\list71\_temp.txt", "wb"); if (NULL != pFile) { fwrite(pBuffer, sizeof(char), lSize, pFile); fclose(pFile); } delete \[\] pBuffer;
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
I don't know. It's not my project. Given that
\r
is a carriage return and\n
is a line feed, I'm not sure what a "line return" is.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Is there a way to create a program that would read a text file and every time it found a space it would replace the space with a line return?
Here's a C++ solution: #include #include using namespace std; ... ifstream in("in.txt"); ofstream out("out.txt"); string s; while(is >> s) os << s << endl;
-
this needs a lot of polish, but it does the bare essentials. in_filename and out_filename could be selected using CFileDialog. they are hard coded here. #include windows.h #include string.h #include stdio.h main() { FILE *infile_p; // pointer for read file FILE *outfile_p; // pointer for write file int byte;// var for byte read from file if ((infile_p = fopen("in_filename", "r"))==NULL) // open file for read { exit(0); } if ((outfile_p = fopen("out_filename", "w"))==NULL) // open file for write { exit(0); } while (!feof(infile_p)) // end of file test { byte = (fgetc(infile_p)); if (byte == 32) // look for a space { fprintf(outfile_p ,"%c%c", 0x0d, 0x0a);/ replace it with CR/LF } else { fprintf( outfile_p, "%c", byte); } } return(0); }