Have you tried to compile this because even without the -Wall flag gcc has a number of complaintsbut then you pass it an int matrixCode:int getNames(char namesArray[][MAX_COLS_NAMES]);Code:int namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
Have you tried to compile this because even without the -Wall flag gcc has a number of complaintsbut then you pass it an int matrixCode:int getNames(char namesArray[][MAX_COLS_NAMES]);Code:int namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
Last edited by hex_dump; 01-30-2013 at 09:52 PM.
look up the scanf and fscanf function. What is the format specifier for reading characters?
once you have that you can put your print statement to print out CHARACTERS, right after you read it in to see if its reading properly. Changed your code and it working on my system.
Last edited by hex_dump; 01-30-2013 at 09:53 PM.
Ohh ok, so I changed the %d to %c which means that now it should be printing characters instead of integers. However, now it doesn't output anything, just a blank space haha.....what do you mean by making my print statement to print out characters?....
OMG.........it actually works!!!! only now, the last problem, how do I get it to look exactly like the txt file? like this:
cause right now, it looks like this:Code:16 Kelly, Victor Lam, Gary Nagasake, David Nguyen, Jeff Nguyen, Michael Sinn, Scott Smith, Jacob Son, Thai Tavares, Maribel Tran, Diane Tsukamoto, Andrew Wang, Mary Young, Daniel Wells, Steve Wong, Justin Johnson, Mary
Code:16Kelly,VictorLam,GaryNagasake,DavidNguyen,JeffNguyen,MichaelSinn,ScottSmith,Jac obSon,ThaiTavares,MaribelTran,DianeTsukamoto,AndrewWang,MaryYoung,DanielWells,St eveWong,JustinJohnson,Mary Process returned 0 (0x0) execution time : 0.297 s Press any key to continue.
Code:if(namesArray[i][j]==',') printf("\n");
Woops, I dont know why I thought the file was like this
Code:Kelly Victor, ... .. ... Tsukamoto Andrew, Wang Mary, Young Daniel, Wells Steve, Wong Justin, Johnson Mary,
ya im on a windows machine and here's my code:
Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 //Function Declarations char getNames(char namesArray[][MAX_COLS_NAMES]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int i = 0; int j = 0; int row = 25; int col = 20; //Statements getNames (namesArray); return 0; } char getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp; int i, j; int col=20; int row=25; //open the sequential access file fp = fopen(INPUT_FILE_GET_NAMES,"r"); if(fp == NULL) { printf("Error, can't open file!!!\n"); exit(101); } else{ for(i=0;i<row;i++) { for(j = 0; j < col; j++) { fscanf(fp,"%c\n", &namesArray[i][j]); printf("%c", namesArray[i][j]); } } } fclose(fp); return 0; }
So your program is not printing newlines to the screen?
Code:fscanf(fp,"%c\n", &namesArray[i][j]); printf("%c", namesArray[i][j]);For plucking single characters out of a text file, try "fgetc()".Originally Posted by fscanf
no it doesnt!! i had to add what @camel-man suggested,However, now it outputs a slightly different output, the commas are misarranged!! it looks like this now!Code:if(namesArray[i][j]==',') printf("\n");
this is my whole program code:Code:16Kelly, VictorLam, GaryNagasake, DavidNguyen, JeffNguyen, MichaelSinn, ScottSmith, JacobSon, ThaiTavares, MaribelTran, DianeTsukamoto, AndrewWang, MaryYoung, DanielWells, SteveWong, JustinJohnson, Mary Process returned 0 (0x0) execution time : 0.078 s Press any key to continue.
How do I use the fgetc()? do i just put that instead of printf()?...Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 //Function Declarations char getNames(char namesArray[][MAX_COLS_NAMES]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int i = 0; int j = 0; int row = 25; int col = 20; //Statements getNames (namesArray); return 0; } char getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp; int i, j; int col=20; int row=25; //open the sequential access file fp = fopen(INPUT_FILE_GET_NAMES,"r"); if(fp == NULL) { printf("Error, can't open file!!!\n"); exit(101); } else{ for(i=0;i<row;i++) { for(j = 0; j < col; j++) { fscanf(fp,"%c\n", &namesArray[i][j]); printf("%c", namesArray[i][j]); if(namesArray[i][j]==',') printf("\n"); } } } fclose(fp); return 0; }
Btw, THANKS GUYS SO MUCH FOR YOUR HELP!!! WOULDNT BE ABLE TO DO THIS WITHOUT YOU! :P
You just need to slow down. Stop adding code all willy-nilly!
You have the input (post #20).
You added the code to print a newline after each comma (post #21), despite the fact that:
1. This post was subsequently corrected as an error (posts #22 and #23)
2. Based on the input (post #20), you shouldn't want a newline after each comma
"fgetc()" (like "fscanf()") reads from an input stream. "printf()" prints to the output. So no, you wouldn't put a read in place of a write.How do I use the fgetc()? do i just put that instead of printf()?...
Did you read the link provided for "fgetc()"? Pay particular attention to the "return value" paragraph.
Using fgets or fgetc will fix all your problems (I would recommend fgets here as you are reading lines, not single characters logically.).
Here is how you would use it in your case:
That should give you an idea of how to get going.Code:char buf[SOME_SIZE]; FILE * fp; char multi_array[5][SOME_SIZE]; if (fp = fopen("somefile.txt", "r")) { int i = sizeof multi_array / sizeof multi_array[0] - 1; while (fgets(buf, sizeof buf, fp) && i >= 0) strcpy(multi_array[i--], buf); fclose(fp); }
@Matticus -- Yes, i just read the link!! I tried doing what it said in the link, but my code got all messed up, so I'm trying to see if nonpuz's way will work....I'm so confused!!
@nonpuz -- I used the exact code you just gave me, and now my program doesnt output anything ((((
Code:#include <stdio.h> #define INPUT_FILE_GET_NAMES "names.txt" #define INPUT_FILE_GET_SALES "sales.txt" #define MAX_ROWS_NAMES 25 #define MAX_COLS_NAMES 20 #define MAX_ROWS_SALES 25 #define MAX_COLS_SALES 6 #define SOME_SIZE 500 //Function Declarations int getNames(char namesArray[][MAX_COLS_NAMES]); int main() { //Local Declarations char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0}; int i = 0; int j = 0; int row = 25; int col = 20; //Statements getNames (namesArray); return 0; } int getNames(char namesArray[][MAX_COLS_NAMES]) { //Statements FILE *fp; int i, j; int col=20; int row=25; char buf[SOME_SIZE]; //open the sequential access file if (fp = fopen(INPUT_FILE_GET_NAMES,"r")){ int i = sizeof namesArray / sizeof namesArray[0] - 1; while (fgets(buf, sizeof buf, fp) && i>=0) strcpy(namesArray[i--], buf); fclose(fp); return 0; } }