Reading from a .txt

This is a discussion on Reading from a .txt within the C Programming forums, part of the General Programming Boards category; Hi, Im new in C and ive got a problem here conserning this topic. I got a a .txt file ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    Reading from a .txt

    Hi,

    Im new in C and ive got a problem here conserning this topic.
    I got a a .txt file like this:

    234 234
    bla bla bla
    bla bla bla
    345436 45645
    435345 34577
    456575 435645
    456455 45646
    bla bla bla


    My problem is that ive got to associate those number to a variable int so that i can use them in another function, they are coordinates of polygons.
    I can open the file and read it but i can associate them to specific int variables like x1,y1,x2,y1, etc.. I understand that i need to use gets and sscanf in a while loop, but get a error saying that im associating a char to a int.

    Can u help me please and send me a exemple code?
    Srry if the message is confusing but i wrote it in a hurry ))

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why don't you post the code that doesn't work and someone can help you figure out why.
    My best code is written with the delete key.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Yes, post your code and we'll tell you what's wrong with it and what needs fixed.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Ok here is the .txt which is actually a .dat file:

    533 1100
    #mapa portugal
    inic_pol
    150 842
    103 1042
    478 1036
    442 845
    fim_pol
    inic_pol
    15 380
    150 842
    407 843
    401 608
    81 376
    fim_pol
    inic_pol
    81 376
    364 583
    328 56
    95 45
    fim_pol


    I have to use g2 library to draw the polygons that im thinking of drawing line to line.
    The code that ive written is now showing trouble compiling, in the console appears :
    /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: ld returned 1 exit status

    Never the less here it goes, the shock to any mediocre programer:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <g2.h>
    #include <g2_PS.h>
    #include <string.h>
    
    
    int main(){
    
    int x, y;
    int x1, y1, x2, y2;
    int i, id;
    char linha[1024];
    char inicio[9] = "inic_pol\n";
    char fim[8] = "fim_pol\n";     
      
    i = 0;
     
            FILE * fp; 
    
    	fp = fopen("mapa1.dat" , "r");
    	if (fp == NULL){
    		printf ("ERRO: ficheiro inexistente\n");
    		return(0);
    	}
    	fgets(linha,[1024],fp);
    
    	sscanf(linha, "%d %d\n", &x, &y);
    	id = g2_open_X11( x , y);
    	
    	if ((strncmp( linha, inicio ))== NULL){
    		while((strncmp( linha, fim ) != NULL){
    			sscanf(linha, "%d %d\n", &x1, &y1);
    			sscanf(linha, "%d %d\n", &x2, &y2);
    			g2_line(id, x1, y1, x2, y2);
    		} 
    			
    		
    			fclose(fp);
     
           }

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Oh yeah! In case you didnt notice XP english is not my mother language... Sorry about that....
    linha -> line
    mapa->map
    inicio->start
    fim->end
    inic_pol-> its like inicializing polygon
    end_pol-> its like ending polygon
    Although i dont believe the translation of the terms is relevant i might give you a help with your intuition.
    Thanks for your help...

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I don't think you have the g2 library installed. If you do, then you're probably not linking it correctly.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    OH ! yeah i havent finished the code yet, much because i cant compile, it keeps giving me that error.
    I using linux

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    how should i link it?
    im writing: gcc -Wall -ansi -pedantic -lm -lg2 -lgd -lX11 -file.c -o file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  2. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  3. reading float from file
    By arjunajay in forum C++ Programming
    Replies: 10
    Last Post: 07-31-2005, 12:01 AM
  4. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 04:54 AM
  5. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21