Need help resolving fscanf and fprintf question
My program is supposed to read 4 numbers from a .txt file, add 10 to the numbers, then print it back. I have a function which loads the text file with four numbers, then another function which adds 10 and appends the file. The program currently works, but I am very confused within my addTen() function.
Why don't I need to fscanf the file again? How does my function already know the values that are held inside my .txt file? I found this accidentally when trying to get my program to work with EOF indicators.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void addTen(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
addTen(text);
fclose(text);
return 0;
} // end main function
void addTen(FILE *file){
int i=0;
int number[4];
for (i=0;i<4;i++)
{
fprintf(file, "%dn", number[i]+10);
printf("nt%d was written to the filen", number[i]+10);
}
}
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("nt%d was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
c scanf
add a comment |
My program is supposed to read 4 numbers from a .txt file, add 10 to the numbers, then print it back. I have a function which loads the text file with four numbers, then another function which adds 10 and appends the file. The program currently works, but I am very confused within my addTen() function.
Why don't I need to fscanf the file again? How does my function already know the values that are held inside my .txt file? I found this accidentally when trying to get my program to work with EOF indicators.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void addTen(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
addTen(text);
fclose(text);
return 0;
} // end main function
void addTen(FILE *file){
int i=0;
int number[4];
for (i=0;i<4;i++)
{
fprintf(file, "%dn", number[i]+10);
printf("nt%d was written to the filen", number[i]+10);
}
}
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("nt%d was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
c scanf
2
I don't see anyfscanf()
s in your code
– Ryan Haining
Nov 20 '18 at 21:04
1
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29
add a comment |
My program is supposed to read 4 numbers from a .txt file, add 10 to the numbers, then print it back. I have a function which loads the text file with four numbers, then another function which adds 10 and appends the file. The program currently works, but I am very confused within my addTen() function.
Why don't I need to fscanf the file again? How does my function already know the values that are held inside my .txt file? I found this accidentally when trying to get my program to work with EOF indicators.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void addTen(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
addTen(text);
fclose(text);
return 0;
} // end main function
void addTen(FILE *file){
int i=0;
int number[4];
for (i=0;i<4;i++)
{
fprintf(file, "%dn", number[i]+10);
printf("nt%d was written to the filen", number[i]+10);
}
}
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("nt%d was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
c scanf
My program is supposed to read 4 numbers from a .txt file, add 10 to the numbers, then print it back. I have a function which loads the text file with four numbers, then another function which adds 10 and appends the file. The program currently works, but I am very confused within my addTen() function.
Why don't I need to fscanf the file again? How does my function already know the values that are held inside my .txt file? I found this accidentally when trying to get my program to work with EOF indicators.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void addTen(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
addTen(text);
fclose(text);
return 0;
} // end main function
void addTen(FILE *file){
int i=0;
int number[4];
for (i=0;i<4;i++)
{
fprintf(file, "%dn", number[i]+10);
printf("nt%d was written to the filen", number[i]+10);
}
}
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("nt%d was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
c scanf
c scanf
edited Nov 21 '18 at 5:08
chux
81.6k871148
81.6k871148
asked Nov 20 '18 at 20:59
Arthur GreenArthur Green
256
256
2
I don't see anyfscanf()
s in your code
– Ryan Haining
Nov 20 '18 at 21:04
1
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29
add a comment |
2
I don't see anyfscanf()
s in your code
– Ryan Haining
Nov 20 '18 at 21:04
1
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29
2
2
I don't see any
fscanf()
s in your code– Ryan Haining
Nov 20 '18 at 21:04
I don't see any
fscanf()
s in your code– Ryan Haining
Nov 20 '18 at 21:04
1
1
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29
add a comment |
3 Answers
3
active
oldest
votes
The program currently works
No, it doesn't!
Your addTen
function has undefined behavior. It is outputting values from an uninitialized array. If it "works" for you, that's probably because it just happens to be in the same part of the stack as the local variable numberArray
that you populate in loadTextFile
before discarding. You cannot rely on this at all.
I suggest that you pass an array into your functions:
void addTen(int array, int size) {
for (int i=0; i < size; i++) {
array[i] += 10;
}
}
void loadTextFile(FILE *file, int array, int size) {
for (int i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
And maybe a separate function to print the data:
void printArray(FILE* fp, int array, int size) {
for (int i=0; i < size; i++) {
fprintf("%dn", array[i]);
}
}
Now invoke it all:
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Note that you have some other errors in your program framework. One issue is that you are opening the input file with openmode "w"
, which will open the file for write-only, and truncate its current contents.
It may be better to open it for read ("r"
) first, load the data and close it. Then open it for write and output the modified data.
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
add a comment |
Turns out I do need a fscanf and the only reason mine worked was because I got lucky! With the help of @paddy and other commentators here, I was able to solve the program. I also solved the program using the idea of pre-loading the text file with specific numbers. The codes below are a refined version of the code posted in the question and will work as a solution to this problem.
//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file, int array, int size);
void printArray(FILE* fp, int array, int size);
void addTen(int array, int size);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","r"); // open the file to read it
int array[4];
loadTextFile(text, array, 4); // scan the .txt file into an array
addTen(array, 4); // add ten to each number in the array
fclose(text); // close the file for writing
fopen("question6.txt","a"); // open the file to append it
printArray(text, array, 4); // print the array to the file
fclose(text); // close the file after appending.
return 0;
} // end main function
void loadTextFile(FILE *file, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
void addTen(int array, int size) {
int i=0;
for (i=0; i < size; i++) {
array[i] += 10;
}
}
void printArray(FILE* fp, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fprintf(fp, "%dn", array[i]);
printf( "%d was written to the array.n", array[i]);
}
}
I have also gotten my original idea to work by pointing the pointer to the beginning of the file. By only opening the file using fopen once, the pointer was at the end of the file at all times.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");
returnStats(text1);
return 0;
} // end main function
void returnStats(FILE *file){
int i=0, count;
int number[4];
while(fscanf(file, "%d", &number[i++])!=EOF){
printf("n%d : Was Read from the Filen", number[i-1]);
count++;
}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
fprintf(text1, "%dn", number[i]+10);
printf("n%d: was appended to the text filen", number[i]+10);
}
} // end returnStats
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("n%d : was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
1
if (text == NULL)
is a good test, but proceeding then toloadTextFile(text);
is pointless. Perhaps return or exit instead.
– chux
Nov 22 '18 at 13:35
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop shouldfscanf()
encounter non-numeric text.while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Alsowhile (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist.while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.
– chux
Nov 22 '18 at 13:39
add a comment |
Your loadTextFile(FILE *file) has an array with 4 integers. It is not reading numbers from the file, it is writing numbers to the file. It has this array: int numberArray[4]={56, 23, 89, 30};
For reading from file you need to use fscanf()
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401438%2fneed-help-resolving-fscanf-and-fprintf-question%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The program currently works
No, it doesn't!
Your addTen
function has undefined behavior. It is outputting values from an uninitialized array. If it "works" for you, that's probably because it just happens to be in the same part of the stack as the local variable numberArray
that you populate in loadTextFile
before discarding. You cannot rely on this at all.
I suggest that you pass an array into your functions:
void addTen(int array, int size) {
for (int i=0; i < size; i++) {
array[i] += 10;
}
}
void loadTextFile(FILE *file, int array, int size) {
for (int i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
And maybe a separate function to print the data:
void printArray(FILE* fp, int array, int size) {
for (int i=0; i < size; i++) {
fprintf("%dn", array[i]);
}
}
Now invoke it all:
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Note that you have some other errors in your program framework. One issue is that you are opening the input file with openmode "w"
, which will open the file for write-only, and truncate its current contents.
It may be better to open it for read ("r"
) first, load the data and close it. Then open it for write and output the modified data.
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
add a comment |
The program currently works
No, it doesn't!
Your addTen
function has undefined behavior. It is outputting values from an uninitialized array. If it "works" for you, that's probably because it just happens to be in the same part of the stack as the local variable numberArray
that you populate in loadTextFile
before discarding. You cannot rely on this at all.
I suggest that you pass an array into your functions:
void addTen(int array, int size) {
for (int i=0; i < size; i++) {
array[i] += 10;
}
}
void loadTextFile(FILE *file, int array, int size) {
for (int i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
And maybe a separate function to print the data:
void printArray(FILE* fp, int array, int size) {
for (int i=0; i < size; i++) {
fprintf("%dn", array[i]);
}
}
Now invoke it all:
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Note that you have some other errors in your program framework. One issue is that you are opening the input file with openmode "w"
, which will open the file for write-only, and truncate its current contents.
It may be better to open it for read ("r"
) first, load the data and close it. Then open it for write and output the modified data.
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
add a comment |
The program currently works
No, it doesn't!
Your addTen
function has undefined behavior. It is outputting values from an uninitialized array. If it "works" for you, that's probably because it just happens to be in the same part of the stack as the local variable numberArray
that you populate in loadTextFile
before discarding. You cannot rely on this at all.
I suggest that you pass an array into your functions:
void addTen(int array, int size) {
for (int i=0; i < size; i++) {
array[i] += 10;
}
}
void loadTextFile(FILE *file, int array, int size) {
for (int i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
And maybe a separate function to print the data:
void printArray(FILE* fp, int array, int size) {
for (int i=0; i < size; i++) {
fprintf("%dn", array[i]);
}
}
Now invoke it all:
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Note that you have some other errors in your program framework. One issue is that you are opening the input file with openmode "w"
, which will open the file for write-only, and truncate its current contents.
It may be better to open it for read ("r"
) first, load the data and close it. Then open it for write and output the modified data.
The program currently works
No, it doesn't!
Your addTen
function has undefined behavior. It is outputting values from an uninitialized array. If it "works" for you, that's probably because it just happens to be in the same part of the stack as the local variable numberArray
that you populate in loadTextFile
before discarding. You cannot rely on this at all.
I suggest that you pass an array into your functions:
void addTen(int array, int size) {
for (int i=0; i < size; i++) {
array[i] += 10;
}
}
void loadTextFile(FILE *file, int array, int size) {
for (int i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
And maybe a separate function to print the data:
void printArray(FILE* fp, int array, int size) {
for (int i=0; i < size; i++) {
fprintf("%dn", array[i]);
}
}
Now invoke it all:
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Note that you have some other errors in your program framework. One issue is that you are opening the input file with openmode "w"
, which will open the file for write-only, and truncate its current contents.
It may be better to open it for read ("r"
) first, load the data and close it. Then open it for write and output the modified data.
edited Nov 20 '18 at 21:33
answered Nov 20 '18 at 21:23
paddypaddy
42.7k53076
42.7k53076
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
add a comment |
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
I got it working using your advice! I will post the updated code in my original post.
– Arthur Green
Nov 20 '18 at 22:01
add a comment |
Turns out I do need a fscanf and the only reason mine worked was because I got lucky! With the help of @paddy and other commentators here, I was able to solve the program. I also solved the program using the idea of pre-loading the text file with specific numbers. The codes below are a refined version of the code posted in the question and will work as a solution to this problem.
//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file, int array, int size);
void printArray(FILE* fp, int array, int size);
void addTen(int array, int size);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","r"); // open the file to read it
int array[4];
loadTextFile(text, array, 4); // scan the .txt file into an array
addTen(array, 4); // add ten to each number in the array
fclose(text); // close the file for writing
fopen("question6.txt","a"); // open the file to append it
printArray(text, array, 4); // print the array to the file
fclose(text); // close the file after appending.
return 0;
} // end main function
void loadTextFile(FILE *file, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
void addTen(int array, int size) {
int i=0;
for (i=0; i < size; i++) {
array[i] += 10;
}
}
void printArray(FILE* fp, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fprintf(fp, "%dn", array[i]);
printf( "%d was written to the array.n", array[i]);
}
}
I have also gotten my original idea to work by pointing the pointer to the beginning of the file. By only opening the file using fopen once, the pointer was at the end of the file at all times.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");
returnStats(text1);
return 0;
} // end main function
void returnStats(FILE *file){
int i=0, count;
int number[4];
while(fscanf(file, "%d", &number[i++])!=EOF){
printf("n%d : Was Read from the Filen", number[i-1]);
count++;
}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
fprintf(text1, "%dn", number[i]+10);
printf("n%d: was appended to the text filen", number[i]+10);
}
} // end returnStats
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("n%d : was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
1
if (text == NULL)
is a good test, but proceeding then toloadTextFile(text);
is pointless. Perhaps return or exit instead.
– chux
Nov 22 '18 at 13:35
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop shouldfscanf()
encounter non-numeric text.while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Alsowhile (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist.while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.
– chux
Nov 22 '18 at 13:39
add a comment |
Turns out I do need a fscanf and the only reason mine worked was because I got lucky! With the help of @paddy and other commentators here, I was able to solve the program. I also solved the program using the idea of pre-loading the text file with specific numbers. The codes below are a refined version of the code posted in the question and will work as a solution to this problem.
//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file, int array, int size);
void printArray(FILE* fp, int array, int size);
void addTen(int array, int size);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","r"); // open the file to read it
int array[4];
loadTextFile(text, array, 4); // scan the .txt file into an array
addTen(array, 4); // add ten to each number in the array
fclose(text); // close the file for writing
fopen("question6.txt","a"); // open the file to append it
printArray(text, array, 4); // print the array to the file
fclose(text); // close the file after appending.
return 0;
} // end main function
void loadTextFile(FILE *file, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
void addTen(int array, int size) {
int i=0;
for (i=0; i < size; i++) {
array[i] += 10;
}
}
void printArray(FILE* fp, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fprintf(fp, "%dn", array[i]);
printf( "%d was written to the array.n", array[i]);
}
}
I have also gotten my original idea to work by pointing the pointer to the beginning of the file. By only opening the file using fopen once, the pointer was at the end of the file at all times.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");
returnStats(text1);
return 0;
} // end main function
void returnStats(FILE *file){
int i=0, count;
int number[4];
while(fscanf(file, "%d", &number[i++])!=EOF){
printf("n%d : Was Read from the Filen", number[i-1]);
count++;
}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
fprintf(text1, "%dn", number[i]+10);
printf("n%d: was appended to the text filen", number[i]+10);
}
} // end returnStats
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("n%d : was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
1
if (text == NULL)
is a good test, but proceeding then toloadTextFile(text);
is pointless. Perhaps return or exit instead.
– chux
Nov 22 '18 at 13:35
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop shouldfscanf()
encounter non-numeric text.while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Alsowhile (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist.while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.
– chux
Nov 22 '18 at 13:39
add a comment |
Turns out I do need a fscanf and the only reason mine worked was because I got lucky! With the help of @paddy and other commentators here, I was able to solve the program. I also solved the program using the idea of pre-loading the text file with specific numbers. The codes below are a refined version of the code posted in the question and will work as a solution to this problem.
//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file, int array, int size);
void printArray(FILE* fp, int array, int size);
void addTen(int array, int size);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","r"); // open the file to read it
int array[4];
loadTextFile(text, array, 4); // scan the .txt file into an array
addTen(array, 4); // add ten to each number in the array
fclose(text); // close the file for writing
fopen("question6.txt","a"); // open the file to append it
printArray(text, array, 4); // print the array to the file
fclose(text); // close the file after appending.
return 0;
} // end main function
void loadTextFile(FILE *file, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
void addTen(int array, int size) {
int i=0;
for (i=0; i < size; i++) {
array[i] += 10;
}
}
void printArray(FILE* fp, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fprintf(fp, "%dn", array[i]);
printf( "%d was written to the array.n", array[i]);
}
}
I have also gotten my original idea to work by pointing the pointer to the beginning of the file. By only opening the file using fopen once, the pointer was at the end of the file at all times.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");
returnStats(text1);
return 0;
} // end main function
void returnStats(FILE *file){
int i=0, count;
int number[4];
while(fscanf(file, "%d", &number[i++])!=EOF){
printf("n%d : Was Read from the Filen", number[i-1]);
count++;
}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
fprintf(text1, "%dn", number[i]+10);
printf("n%d: was appended to the text filen", number[i]+10);
}
} // end returnStats
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("n%d : was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
Turns out I do need a fscanf and the only reason mine worked was because I got lucky! With the help of @paddy and other commentators here, I was able to solve the program. I also solved the program using the idea of pre-loading the text file with specific numbers. The codes below are a refined version of the code posted in the question and will work as a solution to this problem.
//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file, int array, int size);
void printArray(FILE* fp, int array, int size);
void addTen(int array, int size);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","r"); // open the file to read it
int array[4];
loadTextFile(text, array, 4); // scan the .txt file into an array
addTen(array, 4); // add ten to each number in the array
fclose(text); // close the file for writing
fopen("question6.txt","a"); // open the file to append it
printArray(text, array, 4); // print the array to the file
fclose(text); // close the file after appending.
return 0;
} // end main function
void loadTextFile(FILE *file, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fscanf(file, "%d", &array[i]);
}
}
void addTen(int array, int size) {
int i=0;
for (i=0; i < size; i++) {
array[i] += 10;
}
}
void printArray(FILE* fp, int array, int size) {
int i=0;
for (i=0; i < size; i++) {
fprintf(fp, "%dn", array[i]);
printf( "%d was written to the array.n", array[i]);
}
}
I have also gotten my original idea to work by pointing the pointer to the beginning of the file. By only opening the file using fopen once, the pointer was at the end of the file at all times.
#include <stdio.h>
// function prototypes
void loadTextFile(FILE *file);
void returnStats(FILE *file);
// begin main function
int main(void){
FILE *text = fopen("question6.txt","w");
if(text == NULL)
{
printf("question6.dat cannot be opened!n");
fprintf(stderr, "Error opening the fil!n");
}
else
printf("nquestion6.dat was opened successfully for Writing.nn");
loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");
returnStats(text1);
return 0;
} // end main function
void returnStats(FILE *file){
int i=0, count;
int number[4];
while(fscanf(file, "%d", &number[i++])!=EOF){
printf("n%d : Was Read from the Filen", number[i-1]);
count++;
}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
fprintf(text1, "%dn", number[i]+10);
printf("n%d: was appended to the text filen", number[i]+10);
}
} // end returnStats
// function to load the .txt file with array values so we may execute main on any computer.
void loadTextFile(FILE *file){
int numberArray[4]={56, 23, 89, 30};
int i=0;
for(i=0;i<4;i++)
{
fprintf(file, "%dn", numberArray[i]);
printf("n%d : was written to the filen", numberArray[i]);
}
printf("nThe data was successfully writtenn");
} // end function loadTextFile
edited Nov 22 '18 at 13:27
answered Nov 21 '18 at 23:17
Arthur GreenArthur Green
256
256
1
if (text == NULL)
is a good test, but proceeding then toloadTextFile(text);
is pointless. Perhaps return or exit instead.
– chux
Nov 22 '18 at 13:35
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop shouldfscanf()
encounter non-numeric text.while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Alsowhile (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist.while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.
– chux
Nov 22 '18 at 13:39
add a comment |
1
if (text == NULL)
is a good test, but proceeding then toloadTextFile(text);
is pointless. Perhaps return or exit instead.
– chux
Nov 22 '18 at 13:35
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop shouldfscanf()
encounter non-numeric text.while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Alsowhile (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist.while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.
– chux
Nov 22 '18 at 13:39
1
1
if (text == NULL)
is a good test, but proceeding then to loadTextFile(text);
is pointless. Perhaps return or exit instead.– chux
Nov 22 '18 at 13:35
if (text == NULL)
is a good test, but proceeding then to loadTextFile(text);
is pointless. Perhaps return or exit instead.– chux
Nov 22 '18 at 13:35
1
1
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop should fscanf()
encounter non-numeric text. while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Also while (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist. while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.– chux
Nov 22 '18 at 13:39
while (fscanf(file, "%d", &number[i++]) != EOF) {
is an infinite loop should fscanf()
encounter non-numeric text. while (fscanf(file, "%d", &number[i++]) == 1) {
is better. Also while (fscanf(file, "%d", &number[i++]) ....) {
is a problem should more than 4 numbers exist. while (i < 4 && fscanf(file, "%d", &number[i++]) == 1) {
solves that.– chux
Nov 22 '18 at 13:39
add a comment |
Your loadTextFile(FILE *file) has an array with 4 integers. It is not reading numbers from the file, it is writing numbers to the file. It has this array: int numberArray[4]={56, 23, 89, 30};
For reading from file you need to use fscanf()
add a comment |
Your loadTextFile(FILE *file) has an array with 4 integers. It is not reading numbers from the file, it is writing numbers to the file. It has this array: int numberArray[4]={56, 23, 89, 30};
For reading from file you need to use fscanf()
add a comment |
Your loadTextFile(FILE *file) has an array with 4 integers. It is not reading numbers from the file, it is writing numbers to the file. It has this array: int numberArray[4]={56, 23, 89, 30};
For reading from file you need to use fscanf()
Your loadTextFile(FILE *file) has an array with 4 integers. It is not reading numbers from the file, it is writing numbers to the file. It has this array: int numberArray[4]={56, 23, 89, 30};
For reading from file you need to use fscanf()
answered Nov 20 '18 at 21:17
skyCodeskyCode
1342
1342
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401438%2fneed-help-resolving-fscanf-and-fprintf-question%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
I don't see any
fscanf()
s in your code– Ryan Haining
Nov 20 '18 at 21:04
1
Arthur Green, If you have an answer to your own question, post it below as an answer. Post rolled-back.
– chux
Nov 21 '18 at 5:08
@chux I had done that on a previous post and a 10k+ point user told me to remove it. I have added a detailed answer on this post.
– Arthur Green
Nov 22 '18 at 13:29