Reading data from a file and printing it

This is a discussion on Reading data from a file and printing it within the C Programming forums, part of the General Programming Boards category; This is very very informative. But in my case I am not defining the strings in the program, I am ...

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    This is very very informative. But in my case I am not defining the strings in the program, I am reading the strings from a file.
    Code:
    fp = fopen("PSV101data.rtf", "r");
    for (i=1; i <= 47; i++)
    {fscanf(fp, "%s\n", a[i]);
    printf("%s/n", a[i]);}
    fclose(fp);
    Only exception is that fgets is probably more applicable than fscanf.

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Or if you just need an array of pointers to char strings it would be:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXLINE 2
    
    int main(void){
    
    	char *myArray[MAXLINE] = {"Hello World!\n", "Great Day!\n"};
    
    	for (int i = 0; i < MAXLINE; i++){
    		printf("%s", myArray[i]);
    	}
    
    	getchar();
    	return (0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    Any suggestions on how to read these strings from a file and store them in a one dimensional array?
    Last edited by Linux Trojan; 07-01-2011 at 07:14 PM.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you even reading this thread? You've been shown at least two different ways to read from files now in this thread.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    This should get you going. Also, you would want to look up making these real C style strings by placing the '\0' at the end of each line.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINE 2
    #define MAXBUF 30
    
    int main(void){
    
    	char *myArray[MAXLINE] = {"Hello World!\n", "Great Day!\n"};
    	char  myFileArray[MAXLINE][MAXBUF]; //Array to hold info from file
    	FILE *myFile;
    
    
    
    	myFile = fopen("myFile.txt", "w"); //open file for write
    	if(myFile){ //ensure file is opened
    		for (int i = 0; i < MAXLINE; i++){
    			fprintf(myFile,"%s", myArray[i]); //write to file
    		}
    		fclose(myFile); //save changes
    	}
    	
    	myFile = fopen("myFile.txt", "r"); //open file for reading
    	if(myFile){
    		for(int i = 0; i < MAXLINE; i++){
    			fgets(myFileArray[i],MAXBUF,myFile); //read lines of file and store in array
    		}
    		fclose(myFile); //close file
    		for (int i = 0; i < MAXLINE; i++){
    			printf("%s", myFileArray[i]); //print what you have read
    		}
    	}
    	
    	getchar();
    	return (0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    ok man, I will take a close good look at it, in detail. Lots of info there. Thanks

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a one dimension char array:
    Code:
    char oneD[numColumns];
    which will hold ONE line of text, less than numColumn char's in length, and the all important end of string marker char: '\0'.

    This is what you need to hold multiple strings, and it is a two dimension char array:

    Code:
    char twoD[numRows][numColumns];
    In C, you may see this written as:

    Code:
    char *twoD[numColumns];
    Which is not the same thing (it being an array of pointers to one dimension char arrays), Oops! Make that a one dimension array of pointers to char), but in C, these can be used in a similar (not quite the same however), way.

    For now, I suggest you concentrate on using the true 2D char array.

    Say you had this 2d char array:

    Code:
    char twoD[3][9] = { { "January"}, {"February"}, {"March"} };
    Note how, in a loop I can refer to any one of these strings.

    Code:
    for(i = 0; i < 3; i++) {
       printf("This is the twoD string #%d, at index #%d: %s\n", i+1, i, twoD[i]); //i will be the row number of the 2D array
    }
    I only want the first subscript of twoD, because that is the row number - the head of the string. I do NOT want the columns, because I'm not printing out the strings, char by char. See what I mean?

    Although I'm printing the strings here, I could just as well be reading in the months of the year data, from a file, using fgets(), as mentioned in a previous post of mine for you.

    That's as clear as I can make it.
    Last edited by Adak; 07-01-2011 at 10:18 PM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    In C, you may see this written as:

    Code:
    char *twoD[numColumns];
    Which is not the same thing (it being an array of pointers to one dimension char arrays), but in C, these are similar things.
    Not exactly. It is a one dimensional array of pointers to char. Whether or not your char* points at a lone character or a group of characters, there is no way to tell (you have to keep track of that yourself).


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, quite. I will edit that, thanks.

  10. #25
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    I got it to work. Here is what the code looks like, I had to edit it a little bit.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLINE 47
    #define MAXBUF 100
    
    int main(void){
    
    	char *myArray[MAXLINE] ;
    	char  myFileArray[MAXLINE][MAXBUF]; //Array to hold info from file
    	int i;
    	FILE *myFile;
    
    	myFile = fopen("PSV101data.rtf", "r"); //open file for reading
    	if(myFile){
    		for(i = 0; i < MAXLINE; i++){
    			fgets(myFileArray[i],MAXBUF,myFile); //read lines of file and store in array
    		}
    		fclose(myFile); //close file
    		for (i = 0; i < MAXLINE; i++){
    			printf("%s", myFileArray[i]); //print what you have read
    		}
    	}
    
    
    	myFile = fopen("PSV101.rtf", "w"); //open file for write
    	if(myFile){ //ensure file is opened
    		for (i = 0; i < MAXLINE; i++){
    			fprintf(myFile,"%s", myFileArray[i]); //write to file
    		}
    		fclose(myFile); //save changes
    	}
    	
    	
    	getchar();
    	return (0);
    }
    What still confuses me a bit is MyFileArray. Its a one dimensional array but it appears that it has to be assigned memory like a matrix? I am not too sure about this part myFileArray[MAXLINE][MAXBUF]; Appears to be a matrix but then acts like a "vector" (as we say in linear algebra) or one dimension in the LOOP. Any help on that would be appreciated.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    MyFileArray[][] is a two dimensional char array, and you are not allocating any memory to it dynamically, but you have to give it some memory as it is declared - otherwise your program would almost surely crash.

    In C, as stated previously, there is a very close relationship, almost incestuous, between an array, and a pointer. The relationship seems even closer with 2 or more dimensions. In fact, when any array is passed to a function as a parameter, it is degraded into just a pointer, and the size of the array, is no longer known in that function - unless you explicitly bring it in as a parameter, as well.

    It takes SOME getting used to, and subtle errors can arise easily as you work with true arrays of a data type, and arrays of pointers to that same data type.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Linux Trojan View Post
    What still confuses me a bit is MyFileArray. Its a one dimensional array but it appears that it has to be assigned memory like a matrix? I am not too sure about this part myFileArray[MAXLINE][MAXBUF]; Appears to be a matrix but then acts like a "vector" (as we say in linear algebra) or one dimension in the LOOP. Any help on that would be appreciated.
    MyFileArray is your array of strings that you said you've been wanting all this time. char [MAXBUF] is a string (a bunch of characters contiguous in memory, which needs a \0 at the end to be a 'proper' string). You then want an array of them, so you tack on an array index [MAXLINE] to get an array of them and you're in.

  13. #28
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    My friends told me that I could make matricies without using pointers. I was hoping I could use C and skip the pointers. All that stuff about data being in one place yet pointing to another space kinda threw me off. But, looks like if I really wanna be good at this I am gonna has to spend some time on pointers. Thanks for the help

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a file and printing out a graph from the data
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 08:32 PM
  2. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 08:02 AM
  3. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 06:26 PM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 09:39 AM

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