Question:
argv and argc to read in files?
Anonymous
2012-12-10 16:52:51 UTC
I'm trying to become familiar with treating pointers as arrays, argv and argc, and just pointers in general. Everytime I try to run this code it compiles but when I run it from the command prompt I get back "Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at fscanf<_iobuf* , SByte* > .. at main in c:\programs\visual studio 2010\argvread\argvread.cpp:line 40 at _mainCRTStartup<>" and I have no idea what that means.

I think it has something to do with the way i'm reading in the file. I'm using Microsoft Visual Studio 2010 but trying to write in C language instead of C++ if that has anything to do with it too. If anyone could help me to see what I'm doing wrong I'd greatly appreciate it. Here is what I have:


#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
using namespace System;

//FUNCTIONS
void find_average(int *sum, int *total, int *average);

int main(int argc, char *argv[])
{
int *arr;
int sum = 0;
//Total is the total number of elements found, not a sum
int total = 0;
int average = 0;

//Make sure there are two arguments, one execution and one input file
if ( argc!= 2 )
{
printf("\nError. Not enough or too many arguments.\n");
printf("There must be the execution and input file.\n");
exit ( EXIT_FAILURE );
}

//Declare a file pointer to open the input file from the command line
FILE *in = fopen( argv[1], "r" );
//Check to see if the file was found
if ( in == NULL )
{
printf("\nInput file not found");
exit (EXIT_FAILURE);
}

//Read in the values from the input file
while ( !feof(in) )
{
if (fscanf(in, "%d", &arr[total]) > 0)
{
//Sum all of the elements and incriment total elements found
sum = sum + arr[total];
total++;
}
}
//Close the file
fclose (in);

//Find the average
find_average(&sum, &total, &average);

//Print results
printf("\nThere were %d entries found.\n", total);
printf("The sum is %d\n", sum);
printf("The average is %d\n", average);

return 0;
}

void find_average(int *sum, int *total, int *average)
{
*average = *sum / *total;
}
Three answers:
2012-12-10 17:25:09 UTC
It doesn't look like you allocated in memory for int *arr;



You need to either make arr an array large enough to read in the file



int arr[6666];



or allocate memory for it.



int *arr = (int *) malloc( sizeof(int) * 6666);



then check that total does not exceed 6666 or whatever number you choose
picciuto
2016-10-15 10:44:39 UTC
seem, argv[0] is the call of your executable software, argv[a million] is the first token or string after it, and argv[2] is the second one token after it. Token for sure is a string. So from my Linux terminal enable's seem at a software: bash-4.2$ cat token.c #comprise int substantial(int argc, char *argv[]){ if (argc>a million) printf("%sn", argv[a million]); go back 0; } word I used an if statement. we will call this methodology token.c. operating it with a controversy for a filename like archives.txt provides us: bash-4.2$ token archives.txt archives.txt operating it without argument provides us: bash-4.2$ token bash-4.2$ bash-4.2$ token archives.txt archives.txt bash-4.2$ token bash-4.2$ It in basic terms works if I honestly have 2 or more effective tokens on the command line (argv[0] is token, argv[a million] is archives.txt). that's the way you take advantage of it.
FatGuy
2012-12-10 16:58:11 UTC
isn't the first thing you execute, other than declarations is

if ( argc!= 2 ) ?

does argc have a value at this point?



then you have

FILE *in = fopen( argv[1], "r" );

does argv have any value at this point?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...