How do I use a users input to open a text file and then load it into a 2D array?









up vote
0
down vote

favorite












This is what I have in my text file.



6814,85,86,92,88
7234,76,81,84,78
6465,87,54,68,72
7899,92,90,88,86
9901,45,78,79,80
8234,77,87,84,98
7934,76,91,84,65
7284,56,81,87,98
7654,76,87,84,88
3534,86,81,84,73


And this is what I've coded.



void getName(float arr1[x][y])

FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

fscanf(graFile, "%d%d%d%d%d%f", &studentId, &test1, &test2, &test3, &test4, &arr2[i][j]);
arr2[0][0] = studentId;
arr2[0][1] = test1;
arr2[0][2] = test2;
arr2[0][3] = test3;
arr2[0][4] = test4;


for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%f", arr2[i][j]);

printf("n");

fclose(graFile);
return;



I have to write a program that asks the user to input the name of a text file (which contains grades), then load it into a 2D array. Then I have to sort the grades and take the average of the grades. I'm starting with my first function, which is to get the user input for a text file name and load it into a 2D array. I'm still new to C programming and I'm having a hard time understanding 2D arrays. I appreciate any help I can get.










share|improve this question



















  • 1




    Please provide your code in text form in your post
    – Yastanub
    Nov 10 at 0:43






  • 1




    it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
    – landru27
    Nov 10 at 0:44










  • Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
    – user3629249
    Nov 10 at 0:50










  • are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
    – user3629249
    Nov 10 at 0:53






  • 1




    regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
    – user3629249
    Nov 10 at 1:14















up vote
0
down vote

favorite












This is what I have in my text file.



6814,85,86,92,88
7234,76,81,84,78
6465,87,54,68,72
7899,92,90,88,86
9901,45,78,79,80
8234,77,87,84,98
7934,76,91,84,65
7284,56,81,87,98
7654,76,87,84,88
3534,86,81,84,73


And this is what I've coded.



void getName(float arr1[x][y])

FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

fscanf(graFile, "%d%d%d%d%d%f", &studentId, &test1, &test2, &test3, &test4, &arr2[i][j]);
arr2[0][0] = studentId;
arr2[0][1] = test1;
arr2[0][2] = test2;
arr2[0][3] = test3;
arr2[0][4] = test4;


for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%f", arr2[i][j]);

printf("n");

fclose(graFile);
return;



I have to write a program that asks the user to input the name of a text file (which contains grades), then load it into a 2D array. Then I have to sort the grades and take the average of the grades. I'm starting with my first function, which is to get the user input for a text file name and load it into a 2D array. I'm still new to C programming and I'm having a hard time understanding 2D arrays. I appreciate any help I can get.










share|improve this question



















  • 1




    Please provide your code in text form in your post
    – Yastanub
    Nov 10 at 0:43






  • 1




    it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
    – landru27
    Nov 10 at 0:44










  • Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
    – user3629249
    Nov 10 at 0:50










  • are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
    – user3629249
    Nov 10 at 0:53






  • 1




    regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
    – user3629249
    Nov 10 at 1:14













up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is what I have in my text file.



6814,85,86,92,88
7234,76,81,84,78
6465,87,54,68,72
7899,92,90,88,86
9901,45,78,79,80
8234,77,87,84,98
7934,76,91,84,65
7284,56,81,87,98
7654,76,87,84,88
3534,86,81,84,73


And this is what I've coded.



void getName(float arr1[x][y])

FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

fscanf(graFile, "%d%d%d%d%d%f", &studentId, &test1, &test2, &test3, &test4, &arr2[i][j]);
arr2[0][0] = studentId;
arr2[0][1] = test1;
arr2[0][2] = test2;
arr2[0][3] = test3;
arr2[0][4] = test4;


for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%f", arr2[i][j]);

printf("n");

fclose(graFile);
return;



I have to write a program that asks the user to input the name of a text file (which contains grades), then load it into a 2D array. Then I have to sort the grades and take the average of the grades. I'm starting with my first function, which is to get the user input for a text file name and load it into a 2D array. I'm still new to C programming and I'm having a hard time understanding 2D arrays. I appreciate any help I can get.










share|improve this question















This is what I have in my text file.



6814,85,86,92,88
7234,76,81,84,78
6465,87,54,68,72
7899,92,90,88,86
9901,45,78,79,80
8234,77,87,84,98
7934,76,91,84,65
7284,56,81,87,98
7654,76,87,84,88
3534,86,81,84,73


And this is what I've coded.



void getName(float arr1[x][y])

FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

fscanf(graFile, "%d%d%d%d%d%f", &studentId, &test1, &test2, &test3, &test4, &arr2[i][j]);
arr2[0][0] = studentId;
arr2[0][1] = test1;
arr2[0][2] = test2;
arr2[0][3] = test3;
arr2[0][4] = test4;


for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%f", arr2[i][j]);

printf("n");

fclose(graFile);
return;



I have to write a program that asks the user to input the name of a text file (which contains grades), then load it into a 2D array. Then I have to sort the grades and take the average of the grades. I'm starting with my first function, which is to get the user input for a text file name and load it into a 2D array. I'm still new to C programming and I'm having a hard time understanding 2D arrays. I appreciate any help I can get.







c arrays file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 0:59

























asked Nov 10 at 0:39









Angie

12




12







  • 1




    Please provide your code in text form in your post
    – Yastanub
    Nov 10 at 0:43






  • 1




    it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
    – landru27
    Nov 10 at 0:44










  • Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
    – user3629249
    Nov 10 at 0:50










  • are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
    – user3629249
    Nov 10 at 0:53






  • 1




    regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
    – user3629249
    Nov 10 at 1:14













  • 1




    Please provide your code in text form in your post
    – Yastanub
    Nov 10 at 0:43






  • 1




    it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
    – landru27
    Nov 10 at 0:44










  • Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
    – user3629249
    Nov 10 at 0:50










  • are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
    – user3629249
    Nov 10 at 0:53






  • 1




    regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
    – user3629249
    Nov 10 at 1:14








1




1




Please provide your code in text form in your post
– Yastanub
Nov 10 at 0:43




Please provide your code in text form in your post
– Yastanub
Nov 10 at 0:43




1




1




it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
– landru27
Nov 10 at 0:44




it is far, far easier for others to help when your source code and inputs and outputs are in the body of your question, as text, not as images; I have to think it would have been fewer steps for you, as well, to paste what is already text into the text of your question; also, please clarify exactly what isn't working
– landru27
Nov 10 at 0:44












Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
– user3629249
Nov 10 at 0:50




Please post a Minimal, Complete, and Verifiable example of what you have tried and exactly why it is not doing what you want
– user3629249
Nov 10 at 0:50












are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
– user3629249
Nov 10 at 0:53




are x and y global variables? MUCH better to modify the function signature to: void getarray( int x, int y, arry[y] )
– user3629249
Nov 10 at 0:53




1




1




regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
– user3629249
Nov 10 at 1:14





regarding: arr2[0][0] = studentId; arr2[0][1] = test1; arr2[0][2] = test2; arr2[0][3] = test3; arr2[0][4] = test4; This will always copy into the first record in the arr2 suggest: arr2[i][0] = studentId; arr2[i][1] = test1; arr2[i][2] = test2; arr2[i][3] = test3; arr2[i][4] = test4; and when reading the data, this statement: for(int j = 0; j < y; j++) { is not correct and should be removed
– user3629249
Nov 10 at 1:14













1 Answer
1






active

oldest

votes

















up vote
0
down vote













the following proposed code:



  1. cleanly compiles

  2. performs the desired functionality

  3. properly 'types' variables for the expected input

  4. properly checks for errors

  5. uses puts() when appropriate

  6. properly outputs error messages to stderr

  7. avoids unnecessary variable declarations and data copying

  8. consistently indents the code

  9. uses horizontal spacing, when appropriate, for readability

  10. properly uses a format string in calls to printf()

  11. separates code blocks: for if else while do...while switch case, default via a single blank line, for readability

and now, the proposed code:



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

#define MAX_FILENAME_LEN 50

void getName( int x, int y )

int arr2[x][y];
char userIn[ MAX_FILENAME_LEN ];
FILE* graFile;

printf("%s", "Enter filename: ");
if( scanf("%49s", userIn) != 1)

fprintf( stderr, "scanf failed to read file namen" );
exit( EXIT_FAILURE );


// implied else, scanf successful

graFile = fopen(userIn, "r");

if( !graFile )

perror( "fopen failed" );
exit( EXIT_FAILURE );


// implied else, fopen successful

for(int i = 0; i < x; i++)

if( fscanf( graFile,
"%d%d%d%d%d",
&arr2[i][0],
&arr2[i][1],
&arr2[i][2],
&arr2[i][3],
&arr2[i][4] ) != 5 )

fclose( graFile );
fprintf( stderr, "fscanf failed to read row %d from the input filen", i );
exit( EXIT_FAILURE );



fclose(graFile);

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%d", arr2[i][j]);


puts( "" );







share|improve this answer




















  • Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
    – Angie
    Nov 10 at 6:40










  • Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
    – David C. Rankin
    Nov 10 at 7:25











  • @Angie - short example, Read from filename given as argument, or prompt until valid filename given
    – David C. Rankin
    Nov 10 at 8:12










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234997%2fhow-do-i-use-a-users-input-to-open-a-text-file-and-then-load-it-into-a-2d-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













the following proposed code:



  1. cleanly compiles

  2. performs the desired functionality

  3. properly 'types' variables for the expected input

  4. properly checks for errors

  5. uses puts() when appropriate

  6. properly outputs error messages to stderr

  7. avoids unnecessary variable declarations and data copying

  8. consistently indents the code

  9. uses horizontal spacing, when appropriate, for readability

  10. properly uses a format string in calls to printf()

  11. separates code blocks: for if else while do...while switch case, default via a single blank line, for readability

and now, the proposed code:



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

#define MAX_FILENAME_LEN 50

void getName( int x, int y )

int arr2[x][y];
char userIn[ MAX_FILENAME_LEN ];
FILE* graFile;

printf("%s", "Enter filename: ");
if( scanf("%49s", userIn) != 1)

fprintf( stderr, "scanf failed to read file namen" );
exit( EXIT_FAILURE );


// implied else, scanf successful

graFile = fopen(userIn, "r");

if( !graFile )

perror( "fopen failed" );
exit( EXIT_FAILURE );


// implied else, fopen successful

for(int i = 0; i < x; i++)

if( fscanf( graFile,
"%d%d%d%d%d",
&arr2[i][0],
&arr2[i][1],
&arr2[i][2],
&arr2[i][3],
&arr2[i][4] ) != 5 )

fclose( graFile );
fprintf( stderr, "fscanf failed to read row %d from the input filen", i );
exit( EXIT_FAILURE );



fclose(graFile);

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%d", arr2[i][j]);


puts( "" );







share|improve this answer




















  • Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
    – Angie
    Nov 10 at 6:40










  • Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
    – David C. Rankin
    Nov 10 at 7:25











  • @Angie - short example, Read from filename given as argument, or prompt until valid filename given
    – David C. Rankin
    Nov 10 at 8:12














up vote
0
down vote













the following proposed code:



  1. cleanly compiles

  2. performs the desired functionality

  3. properly 'types' variables for the expected input

  4. properly checks for errors

  5. uses puts() when appropriate

  6. properly outputs error messages to stderr

  7. avoids unnecessary variable declarations and data copying

  8. consistently indents the code

  9. uses horizontal spacing, when appropriate, for readability

  10. properly uses a format string in calls to printf()

  11. separates code blocks: for if else while do...while switch case, default via a single blank line, for readability

and now, the proposed code:



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

#define MAX_FILENAME_LEN 50

void getName( int x, int y )

int arr2[x][y];
char userIn[ MAX_FILENAME_LEN ];
FILE* graFile;

printf("%s", "Enter filename: ");
if( scanf("%49s", userIn) != 1)

fprintf( stderr, "scanf failed to read file namen" );
exit( EXIT_FAILURE );


// implied else, scanf successful

graFile = fopen(userIn, "r");

if( !graFile )

perror( "fopen failed" );
exit( EXIT_FAILURE );


// implied else, fopen successful

for(int i = 0; i < x; i++)

if( fscanf( graFile,
"%d%d%d%d%d",
&arr2[i][0],
&arr2[i][1],
&arr2[i][2],
&arr2[i][3],
&arr2[i][4] ) != 5 )

fclose( graFile );
fprintf( stderr, "fscanf failed to read row %d from the input filen", i );
exit( EXIT_FAILURE );



fclose(graFile);

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%d", arr2[i][j]);


puts( "" );







share|improve this answer




















  • Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
    – Angie
    Nov 10 at 6:40










  • Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
    – David C. Rankin
    Nov 10 at 7:25











  • @Angie - short example, Read from filename given as argument, or prompt until valid filename given
    – David C. Rankin
    Nov 10 at 8:12












up vote
0
down vote










up vote
0
down vote









the following proposed code:



  1. cleanly compiles

  2. performs the desired functionality

  3. properly 'types' variables for the expected input

  4. properly checks for errors

  5. uses puts() when appropriate

  6. properly outputs error messages to stderr

  7. avoids unnecessary variable declarations and data copying

  8. consistently indents the code

  9. uses horizontal spacing, when appropriate, for readability

  10. properly uses a format string in calls to printf()

  11. separates code blocks: for if else while do...while switch case, default via a single blank line, for readability

and now, the proposed code:



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

#define MAX_FILENAME_LEN 50

void getName( int x, int y )

int arr2[x][y];
char userIn[ MAX_FILENAME_LEN ];
FILE* graFile;

printf("%s", "Enter filename: ");
if( scanf("%49s", userIn) != 1)

fprintf( stderr, "scanf failed to read file namen" );
exit( EXIT_FAILURE );


// implied else, scanf successful

graFile = fopen(userIn, "r");

if( !graFile )

perror( "fopen failed" );
exit( EXIT_FAILURE );


// implied else, fopen successful

for(int i = 0; i < x; i++)

if( fscanf( graFile,
"%d%d%d%d%d",
&arr2[i][0],
&arr2[i][1],
&arr2[i][2],
&arr2[i][3],
&arr2[i][4] ) != 5 )

fclose( graFile );
fprintf( stderr, "fscanf failed to read row %d from the input filen", i );
exit( EXIT_FAILURE );



fclose(graFile);

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%d", arr2[i][j]);


puts( "" );







share|improve this answer












the following proposed code:



  1. cleanly compiles

  2. performs the desired functionality

  3. properly 'types' variables for the expected input

  4. properly checks for errors

  5. uses puts() when appropriate

  6. properly outputs error messages to stderr

  7. avoids unnecessary variable declarations and data copying

  8. consistently indents the code

  9. uses horizontal spacing, when appropriate, for readability

  10. properly uses a format string in calls to printf()

  11. separates code blocks: for if else while do...while switch case, default via a single blank line, for readability

and now, the proposed code:



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

#define MAX_FILENAME_LEN 50

void getName( int x, int y )

int arr2[x][y];
char userIn[ MAX_FILENAME_LEN ];
FILE* graFile;

printf("%s", "Enter filename: ");
if( scanf("%49s", userIn) != 1)

fprintf( stderr, "scanf failed to read file namen" );
exit( EXIT_FAILURE );


// implied else, scanf successful

graFile = fopen(userIn, "r");

if( !graFile )

perror( "fopen failed" );
exit( EXIT_FAILURE );


// implied else, fopen successful

for(int i = 0; i < x; i++)

if( fscanf( graFile,
"%d%d%d%d%d",
&arr2[i][0],
&arr2[i][1],
&arr2[i][2],
&arr2[i][3],
&arr2[i][4] ) != 5 )

fclose( graFile );
fprintf( stderr, "fscanf failed to read row %d from the input filen", i );
exit( EXIT_FAILURE );



fclose(graFile);

for(int i = 0; i < x; i++)

for(int j = 0; j < y; j++)

printf("%d", arr2[i][j]);


puts( "" );








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 1:45









user3629249

10.8k1914




10.8k1914











  • Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
    – Angie
    Nov 10 at 6:40










  • Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
    – David C. Rankin
    Nov 10 at 7:25











  • @Angie - short example, Read from filename given as argument, or prompt until valid filename given
    – David C. Rankin
    Nov 10 at 8:12
















  • Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
    – Angie
    Nov 10 at 6:40










  • Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
    – David C. Rankin
    Nov 10 at 7:25











  • @Angie - short example, Read from filename given as argument, or prompt until valid filename given
    – David C. Rankin
    Nov 10 at 8:12















Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
– Angie
Nov 10 at 6:40




Thank you! This helped me out a lot. If I wanted to keep repeating the program until the user entered the correct file name, how could I do that? I know I would need a while loop but I'm not sure where to put it.
– Angie
Nov 10 at 6:40












Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
– David C. Rankin
Nov 10 at 7:25





Nit "uses puts() when appropriate", presuming you mean puts( "" ); Why are you using a string function to output a single char? Perhaps putchar ('n'); for a single character? Also, better to explain that OP got wrong (though your answer clearly helped).
– David C. Rankin
Nov 10 at 7:25













@Angie - short example, Read from filename given as argument, or prompt until valid filename given
– David C. Rankin
Nov 10 at 8:12




@Angie - short example, Read from filename given as argument, or prompt until valid filename given
– David C. Rankin
Nov 10 at 8:12

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234997%2fhow-do-i-use-a-users-input-to-open-a-text-file-and-then-load-it-into-a-2d-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo