0

I want to cut off everything in a text file after the strings being separated by a space on each line, so if for example the text file contains the Line:

I am a String.
Iam a String too.

There would only be I in the output file on the first line of the output file, and on the second line there only would be Iam left of the line.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *input,*output;
    char line[40];
    if(argc == 3){
        input = fopen(argv[1],"r");
        output = fopen(argv[2],"w");
        if(input!= NULL){
                while(!feof(input)){
                    fgets(line,sizeof line,input);
                    strtok(line," ");                    
                    fputs(line,output);                  

                }
                fclose(input);
                fclose(output);
        }
    }
}

this is how it'S almost completeley is working although its obfuscating more lines with each others :/

StYl3z
  • 11
  • 2
  • 1
    You should distinguish "character" and "string". (String in C is NUL-terminated array of characters.) – MikeCAT Oct 30 '19 at 15:21
  • 1
    I think you simply mean "space", not "backspace". – Federico klez Culloca Oct 30 '19 at 15:23
  • to explain what the code SHOULD do, is reading the textfile character by character into the variable ch and if the current character is a space, which is compared by the function ascii to integer to its value and if it'S a backspace, so as an int '32', it's adding the end-of-string NULL! to the file, but I do not know how it should go on :/ edit:\\ oh, yeah i mean space not backspace :D – StYl3z Oct 30 '19 at 15:24
  • 1
    You should really consider limiting the nesting, as that makes the code harder to read, understand, follow and maintain. – Some programmer dude Oct 30 '19 at 15:26
  • 2
    Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Oct 30 '19 at 15:27
  • 1
    Algorithm you want: 1. read a line. 2. process the line,that is put a `NUL` character at the first occurence of `' '` (space). 3. write the line. Repeat this until the end of the file. Hint 3: read the chapter dealing with strings in your beginner's C text book. – Jabberwocky Oct 30 '19 at 15:33
  • https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Irelia Oct 30 '19 at 15:38
  • I was thinking about using fgets but I do not find the correct way using it in my book @ the library explaining part :/ – StYl3z Oct 30 '19 at 15:40
  • @StYl3z then maybe you should post a question about _that_. – Jabberwocky Oct 30 '19 at 15:43
  • You don't want `strtok` here but the simpler and more appropriate `strchr` here – Jabberwocky Oct 30 '19 at 18:06

1 Answers1

0

You want something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
  if (argc != 3)
  {
    display error message
    return 1;
  }

  FILE *infile = fopen(argv[1], "r");  // Use "r", not 'w'.
                                       // Use consistant variable naming: infile and outfile
  if (infile == NULL)                  // file could be opened?
  {
    display error message
    return 1;
  }

  FILE *outfile = fopen(argv[2], "w");   // Use "w", not 'a'
  if (outfile == NULL)                   // file could be opened?
  {
    display error message

    fclose(infile);                   // close infile that is obviously open here
    return 1;
  }

  do
  {
    char line[1000];

    if (fgets(line, sizeof line, infile) == NULL)   // read complete line
      break;                                        // enf of file : stop loop

      process line

      fputs(line, outfile);                         // write line
  } while (1);

  fclose(infile);
  fclose(outfile);
}

There is still code for you to write. There may be errors in this code, but you should get the idea.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • yeah, saw that it*s ugly putting code in answer and toggling code is not working for any reason. nvm. the solution with strtok using " " as delimiter is cutting it off, but will repeat code like this: I am a String Iam a String Im a String >>> IaaString for any reason, although I read I have to use it multiple times :/ – StYl3z Oct 30 '19 at 16:39