Jump to content

Best way to clean up opened txt file?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Excess3

Excess3

    Newbie

  • Members
  • Pip
  • 5 posts
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

#2
helixed

helixed

    Newbie

  • Members
  • PipPip
  • 17 posts
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:

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
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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:

//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!
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz