-1

I am trying to simply read in a basic text file, split each line into separate strings and rearrange/copy them onto a new text file. Is there any simple way to split and identify these strings to be added to a new file at the end of processing the lines?

My code so far:

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

int main()
{
    FILE *pFileCust
    fPointer = fopen("Athletes.txt", "r");
    char singleLine[150];

    while (!feof(pFileCust)){
        fscanf(singleLine, 150);
        int id, name, sport;
        fprintf(%d[0,6], %s[8,15], %s[16,22], id, name, sport);
    }

    fclose(fPointer);
    return 0;
}

Example Text File to be read into the program:

88888 John Doe Tennis
99999 Jane Smith Softball

Example Output that I am trying to achieve.

Tennis 88888 John Doe
Softball 99999 Jane Smith
shibaking
  • 61
  • 1
  • 6
  • What is your question? – zerkms Sep 14 '17 at 00:00
  • 1
    While this is all possible, perhaps you are using wrong language. If you used Python or Perl, it would be trivial task. E.g. solution using awk is: `awk '{print $4, $1, $2, $3}' < input.txt` – mvp Sep 14 '17 at 00:00
  • sorry zerkms I edited the post! – shibaking Sep 14 '17 at 00:02
  • Is the "example output" what you *want* to get or what you *actually* get? – mkrieger1 Sep 14 '17 at 00:04
  • 1
    Consider refactoring the feof check. [See this question](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Addison Sep 14 '17 at 00:07
  • the example output is what I want to get – shibaking Sep 14 '17 at 00:08
  • Ok, and what do you actually get? – mkrieger1 Sep 14 '17 at 00:09
  • The program does not compile and at the beginning of my attempts it printed 0's very rapidly – shibaking Sep 14 '17 at 00:10
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – mkrieger1 Sep 14 '17 at 00:11
  • 1
    A lot of weird stuff going on here; `fPointer` is undefined, you probably want to use `pFileCust`; `fscanf` should be taking your file handle as 1st argument, then format, then your variables. `fprintf` should be either `STDOUT`. Your variable will need to be defined before `fscanf`. Finally `name` and `sport` aren't integers in your example but you're using `int` type. – Tyler Szabo Sep 14 '17 at 00:17
  • I made a [bit of a template](https://onlinegdb.com/B1h8hSwc-) for you. This might be a better starting place for you @tx203141. Consider using `strtok`, or maybe even `sscanf` to split. Alternately, you could read using `fscanf`? – Addison Sep 14 '17 at 00:28
  • I would love to be able to use strtok effectively I have seen that all over the place for help with this task but I am not sure how to use it correctly, especially after reading in the file – shibaking Sep 14 '17 at 00:37

2 Answers2

0

Each line in your file corresponds to a record. Each series of consecutive non-whitespace characters corresponds to a field in the current record. Accordingly,

/* getrecord: read next record on fp */
char *getrecord(FILE *fp)
{
    assert(fp);

    char *line = malloc(MAXLINE);
    if (line != NULL)
        if (fgets(line, MAXLINE, fp) != NULL)
            return line;

    return NULL;
}

/* getfield: read next field in record */
char *getfield(const char *record, int *pos)
{
    assert(record && pos);

    char *record;
    int ret;

    if ((record = malloc(MAXRECORD)) != NULL) {
        ret = sscanf(record + *pos, "%s", record);
        if (ret == 1)
            return record;
    }
    return NULL;
}

While each function does not appear to do much work, separating your business logic from record/field reading has real benefits. It allows extensibility (for example you can add error handling to these routines). You can also make more sense of your code. Now you can write your main function which will use this pair of calls.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0

Here is a simple adaptation of your code that (a) compiles, and (b) generates the output that you desire.

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

int main()
{
  FILE *pFileCust = fopen("Athletes.txt", "r");
  char first_name[100], last_name[100], sport[100];
  int id;

  while (fscanf(pFileCust, "%d %s %s %s", &id, first_name, last_name, sport) != EOF) {
    printf("%s %d %s %s\n", sport, id, first_name, last_name);

  }

  fclose(pFileCust);
  return 0;
}

One of the key things your app was missing was anything assigning values to your variables; your fscanf was not doing anything (if it even compiled). fscanf is not a very GOOD way to parse text, it's not very robust. But for the principle of understanding how variables are assigned, please look carefully at that, and in particular understand why the id has an ampersand in front of it. Also, make sure you understand why I used both a first name and a last name.

Blunt Jackson
  • 616
  • 4
  • 17