Hello i have two questions:
1- i need to organize data in fopened txt file in c , i saved the needed data in structure array and now need to write it to txt file but one problem - how can i clean up the old data from this(to write new from array) file some function like backspace in c?
2- I opened another file in "read" mode i need to scan line by line how can i jump from one line to start of another>?
tnx for help and happy holydays
Best way to clean up opened txt file?
Started by Excess3, Dec 26 2008 01:24 AM
2 replies to this topic
#1
Posted 26 December 2008 - 01:24 AM
|
|
|
#2
Posted 29 December 2008 - 07:02 PM
Hello,
I'm still beginning at C, but I'll try to help you out if I can. As far as I know a text file is simply a collection of characters. Therefore, the following text:
would be stored in a text file as:
with an end of file character located at the end of the file. Therefore, the computer doesn't distinguish our concept of a line outside of the newline character. If you wanted to jump to the second line of the file, you would have to scan the file until you got to the first newline character.
I'm a little confused by what you mean when you say you want to "clean up" the text file. If, say, for example, you took the following text file:
and you wanted to remove everything that wasn't a letter or a space, you could do something such as the following:
Hope that helps.
helixed
I'm still beginning at C, but I'll try to help you out if I can. As far as I know a text file is simply a collection of characters. Therefore, the following text:
hello world hello world
would be stored in a text file as:
"hello world\nhello worldEOF"
with an end of file character located at the end of the file. Therefore, the computer doesn't distinguish our concept of a line outside of the newline character. If you wanted to jump to the second line of the file, you would have to scan the file until you got to the first newline character.
I'm a little confused by what you mean when you say you want to "clean up" the text file. If, say, for example, you took the following text file:
"hello_!@# world"
and you wanted to remove everything that wasn't a letter or a space, you could do something such as the following:
#include <stdio.h>
#include <ctype.h>
#define INPUT "input.txt"
#define OUTPUT "output.txt"
int main(int argc, char *argv[])
{
//open the files
FILE *inp;
FILE *outp;
inp = fopen(INPUT, "r");
outp = fopen(OUTPUT, "w");
char ch;
//processes the file
//first, scan in a character and check that it's not the end of file character
while ((ch = getc(inp)) != EOF)
{
//next, check to see if the character is a space or a letter of the alphabet
if (ch == ' ' || isalpha(ch))
{
//if so, write ch to the output file
putc(ch, outp);
}
}
//close the files
fclose(inp);
fclose(outp);
return 0;
}
Hope that helps.
helixed
#3
Posted 30 December 2008 - 03:06 PM
1- I assume that what you mean is "How can I get rid of the data in the file, so that I have a fresh, blank file?" This can be done easily. When you open a file, you set it to read, write, or append, by the flags "r" "w" or "a". "r" reads the current data. "w" blanks the file, and you can add new data. "a" adds new data to the end.
2- After reading part of a line, you can use the following function:
Hope that helps. Bai!
2- After reading part of a line, you can use the following function:
//returns number of chars skippped.
int skip_to_new_line(FILE * f){
int c=0,i=0;
while((c = getc(f)) != '\n')i++;
return i;
}
Hope that helps. Bai!


Sign In
Create Account

Back to top









