-1

I have an document with some telephone number and andresses. I now try to copy the numbers in one part of a struct and the adress into another. At the moment I was just able to get the data of the document but I can't put it in my struct please help me

C Code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

struct telefon{
    char nummer[16];
    char adresse[128];
};

typedef struct telefon TELEFON;

void main()
{
    TELEFON tel;
    char buffer[256];
    FILE *fp;
    int i = 0;
    int countSemi = 0;

    fp = fopen("Telefondatei.txt", "r");

    if(fp == NULL) 
    {
        printf("Datei konnte nicht geoeffnet werden.\n");
    }
    else{

        while(fgets(buffer,1000,fp) != 0){
            //printf("%s\n",buffer);
            while(buffer != 0){
                i++;
                if(buffer[i] == ';'){
                    countSemi++;
                }
                while(countSemi <= 7){
                    strcpy(tel.adresse,buffer);
                    printf("%s\n %d \n",tel.adresse,countSemi);
                }
            }
        }
    }
}

Example for the data in my .txt document

"Firma";"";"Auto GmbH";"gasse 3";"5000";"Mon";"";"0456";"45652" "Firma";"";"ADAC";"";"50000";"Mon";"";"2156";"545218"

alovaros
  • 456
  • 4
  • 23
  • 3
    1) If you buffer is 256 bytes, why you want read 1000 in `fgets()`? 2) Why buffer should became NULL in the following `while` loop? - Eventually you want read again docs for `fgets()`? – Frankie_C Aug 30 '15 at 19:29
  • you are totaly rigth with the 1000 bytes that's just usless. My teacher explained us when the document ends the fgets returns null – alovaros Aug 30 '15 at 19:31
  • Which fields in your semicolon separated values file correspond to the phone number, and which to the address? Do you expect to keep the semicolons in between sections of the address? If not, what do you want instead — newlines, spaces, something else? Note that `main()` should normally return an `int` — see [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336) for the details. – Jonathan Leffler Aug 30 '15 at 19:32
  • The `fgets()` returns null on EOF, but you already test (correctly) for that. @Frankie_C was concerned about the `while (buffer != 0)` loop; you may be thinking of `while (buffer[i] != '\0')` as the condition — it would probably be more sensible, at any rate. – Jonathan Leffler Aug 30 '15 at 19:34
  • just the last 2 strings should get in numberI want the rest in adress. hmm I really don't care about semicolons :D I thougth about that I just delet them so I can replace them with nothing or space or something like this – alovaros Aug 30 '15 at 19:35
  • Since you are dealing with what's usually called CSV (comma-separated values), albeit using semicolons instead of commas, you probably need a library to handle the format. The details are tricky. There must be plenty of libraries around. There's workable code described in [The Practice of Programming](http://www.cs.princeton.edu/~bwk/tpop.webpage/). You'd need to fettle it to accept semicolon instead of comma as the field separator. You can then process the fields as you need. It isn't clear how to format the address data for the sample data, though the phone number is the last two fields. – Jonathan Leffler Aug 30 '15 at 19:56

2 Answers2

1

You will need to use strtok additionally. See this example. However, please note this example assumes the data is written in fixed format (and it will not work if data comes in other format - you might want to modify this for your needs, this is just illustration):

Assumed data format for each line:

Address;telephoneNumber;

#include <string.h>
..
char * value;
while(fgets(buffer,256,fp) != 0)
{
   value = strtok(buffer, ";"); // get address
   strcpy(tel.adresse, value);

   value = strtok(NULL, ";"); // get number
   strcpy(tel.nummer, value);

}

Also this:

while(buffer != 0)

in your code doesn't make sense. Hardly buffer will be 0. It is array and value of buffer will always be memory address where that array starts. You can't assign to buffer.

Here is another post about using strtok.

Community
  • 1
  • 1
gmoniava
  • 18,228
  • 5
  • 33
  • 74
  • hmm jeah that's a good first part but I can't change format :D if we just would have adress and number it would be easy – alovaros Aug 30 '15 at 19:41
  • @alovaros: maybe you can apply this approach to your case then. – gmoniava Aug 30 '15 at 19:42
  • Your assumed format looks a little peculiar. For the sample data, there is no phone number in either row. Granted, we have not been told about where the phone number is. If I had to guess, it is the last two columns. But we don't know. – Jonathan Leffler Aug 30 '15 at 19:44
  • @JonathanLeffler:Well, I think with strtok the format needs to be known in advance, isn't it? Maybe OP can apply this to his case. If not, I can remove the answer altogether. – gmoniava Aug 30 '15 at 19:45
-2

To get your data use : fscanf(fp, "%s %s %s %d", str1, str2, str3, &number); or you can use getc to get one character but u need to test the EOF

cip
  • 69
  • 8
  • How would this help? The first data line would populate `str1` and `str2` and leave the others unset. On the second line of data, `str2` would not be set either. – Jonathan Leffler Aug 30 '15 at 19:35
  • No, Just put it in a loop the cursor will be on the next line of data – cip Sep 03 '15 at 01:08
  • I'm sorry, but that's plain nonsense. My MCVE: `#include ` and `int main(void) { FILE *fp = stdin; char str1[80]; char str2[80]; char str3[80]; int number; int line = 0; int rc; while ((rc = fscanf(fp, "%s %s %s %d", str1, str2, str3, &number)) > 0) { printf("Line %d: fields = %d\n", ++line, rc); if (rc >= 1) printf("str1 = [[%s]]\n", str1); if (rc >= 2) printf("str2 = [[%s]]\n", str2); if (rc >= 3) printf("str3 = [[%s]]\n", str3); if (rc >= 4) printf("number = [[%d]]\n", number); } return 0; }` spread over 29 lines in my code. – Jonathan Leffler Sep 03 '15 at 01:48
  • When I run that on the data file shown, I get: `Line 1: fields = 3 str1 = [["Firma";"";"Auto]] str2 = [[GmbH";"gasse]] str3 = [[3";"5000";"Mon";"";"0456";"45652"]] Line 2: fields = 1 str1 = [["Firma";"";"ADAC";"";"50000";"Mon";"";"2156";"545218"]]`. Even allowing for no formatting in comments, that's nothing like what is requested. – Jonathan Leffler Sep 03 '15 at 01:49